Auto rotate while reading images from sd card Android
<p>i'm to Overflow and this is my first question here :)</p>

<p>I have an gallery, image switcher and button for capturing image from camera.
After capturing image into the SD card I want to load the image to the gallery and also show it in the image switch object. for now it work fine with one big question.</p>

<p>Why all the images are rotated in the gallery and the image switcher?</p>

<p>this is my java code :</p>

<pre><code>protected void onCreate(Bundle savedInstanceState) {
   iSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher01);
        iSwitcher.setFactory(this);
        iSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
        iSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));
        setAllPics();
        gallery = (Gallery) findViewById(R.id.Gallery01);
        gallery.setAdapter(new ImageAdapter(this)); 
        gallery.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2,
                    long arg3) {
                //iSwitcher.setImageResource(pics[arg2]);
                //iSwitcher.setImageURI(mUrls[arg2]);
                iSwitcher.setImageDrawable(new BitmapDrawable(getResources(),(bitmap[arg2])));
            }
        });
        Button BtnGetPhoto = (Button) this.findViewById(R.id.BtnGetPicture);
}
public class ImageAdapter extends BaseAdapter {

        private Context ctx;
        int mGalleryItemBackground;
        public ImageAdapter(Context c) {
            ctx = c;
        }

        @Override
        public int getCount() {
            return mUrls.length;
            //return pics.length;
        }

        @Override
        public Object getItem(int arg0) {

            return arg0;
        }

        @Override
        public long getItemId(int arg0) {

            return arg0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {

            ImageView iView = new ImageView(ctx);
            //iView.setImageURI(mUrls[arg0]);
            iView.setImageDrawable(new BitmapDrawable(getResources(),(bitmap[arg0])));
            //iView.setImageResource(pics[arg0]);
            iView.setScaleType(ImageView.ScaleType.FIT_XY);
            iView.setLayoutParams(new Gallery.LayoutParams(150, 150));
            return iView;
        }
    }
private void setAllPics(){
        File images  = new File(PATH);
        File[] imagelist = images.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String filename) {
                 return ((filename.endsWith(".jpg"))||(filename.endsWith(".png")));  
            }
        });
        /* There isn't enough memory to open up more than a couple camera photos */
        /* So pre-scale the target bitmap into which the file is decoded */

        /* Get the size of the ImageView */
        int targetW = 350;//iSwitcher.getWidth();
        int targetH = 220;//iSwitcher.getHeight();


        BitmapFactory.Options bmOptions = new BitmapFactory.Options();



        mFiles = new String[imagelist.length];
        int photoW[] = null;
        int photoH[] = null; 
        int scaleFactor[] = null ;

        photoW = new int[imagelist.length];
        photoH = new int[imagelist.length];
        scaleFactor = new int[imagelist.length];
        bitmap = new Bitmap[imagelist.length];
        for(int i= 0 ; i&lt; imagelist.length; i++)  
            {               
                bmOptions.inJustDecodeBounds = true;
                mFiles[i] = imagelist[i].getAbsolutePath();  
                bitmap[i] = BitmapFactory.decodeFile(mFiles[i], bmOptions);
                /* Get the size of the image */
                photoW[i] = bmOptions.outWidth;
                photoH[i] = bmOptions.outHeight;
                /* Figure out which way needs to be reduced less */

                if((targetW &gt; 0)  || (targetH &gt; 0 ))
                    scaleFactor[i]  = Math.min(photoW[i]/targetW, photoH[i]/targetH);
                /* Set bitmap options to scale the image decode target */
                bmOptions.inJustDecodeBounds = false;
                bmOptions.inSampleSize = scaleFactor[i];
                bmOptions.inPurgeable = true;
                try{
                /* Decode the JPEG file into a Bitmap */
                bitmap[i] = BitmapFactory.decodeFile(mFiles[i], bmOptions);
                //iSwitcher.setImageDrawable(new BitmapDrawable(getResources(),(bitmap[i])));
                } catch (OutOfMemoryError error) {
                    error.printStackTrace();
                }

            }  


        mUrls = new Uri[mFiles.length];  

        for(int i=0; i &lt; mFiles.length; i++)  
                {  
                    mUrls[i] = Uri.parse(mFiles[i]);     
                }


    }
private OnClickListener onClickListener =new OnClickListener(){ 

    // @Override
     public void onClick(View v) {

         switch (v.getId()){
             case R.id.BtnGetPicture:

                 Intent cameraIntent = new Intent (android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                 String imageFileName = "";
                 String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());


                 SetPathIfNotExist(PATH);
                 imageFileName = timeStamp + ".jpg";

                 File image = new File(PATH,imageFileName);
                 Uri uriSavedImage = Uri.fromFile(image);
                 cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

                 startActivityForResult(cameraIntent,CAMERA_REQUEST);
                     break;
                        default:

                break;
         }          

     }    
     };
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST &amp;&amp; resultCode == RESULT_OK) { 
        setAllPics();
        gallery.setAdapter(new ImageAdapter(this)); 

        //Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        //imageView.setImageBitmap(photo);
    }  
} 
</code></pre>
