0

I have made an app in which user selects an image from gallery and that image is shown in the imageview.I have used resizing method to avoid Out of Memory issue.The problem is that when i select image which is having lower width and height the image is being properly set in the imageview,but when image width and height is large the image is rotated and set in the imageview.Why this is happening.Please help

CaptureImage

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult (requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK && null != data) {


            selectedImage = data.getData ();
            try {
                resizedGallery = decodeUri (getApplicationContext (), selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace ();
            }

            dialog = new Dialog (CaptureImage.this, android.R.style.Theme_DeviceDefault);
            dialog.requestWindowFeature (Window.FEATURE_NO_TITLE);
            dialog.setContentView (R.layout.confirmation_image);
            dialog.getWindow ().setLayout (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            ImageView image = (ImageView) dialog.findViewById (R.id.iv_selected_image);
            image.setImageBitmap (resizedGallery);
            image.setRotation (90);
            ok_gallery = (Button) dialog.findViewById (R.id.bt_ok_gallery);
            cancel = (Button) dialog.findViewById (R.id.bt_cancel);
            ok_gallery.setOnClickListener (this);
            cancel.setOnClickListener (this);
            dialog.show (); 

decodeUri Method

public static Bitmap decodeUri(Context c, Uri uri)
            throws FileNotFoundException {
        BitmapFactory.Options o = new BitmapFactory.Options ();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream (c.getContentResolver ().openInputStream (uri), null, o);

        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;

        while (true) {
            if (width_tmp / 2 < 400 || height_tmp / 2 < 200)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options ();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream (c.getContentResolver ().openInputStream (uri), null, o2);
    } 
Vishal
  • 11
  • 4

1 Answers1

0

Its happening because of calling image.setRotation(90), after setting bitmap to imageview; Remove it and try again.

Audumbar
  • 404
  • 5
  • 16
  • By the i want to set all the image from the gallery to be of same widht and height,how can i achive it???? – Vishal May 29 '14 at 06:39
  • How image rotation can depends on its size? – Audumbar May 29 '14 at 06:41
  • when i select an img from gallery which is taken from the camera(12mp) my img gets rotated in imageView – Vishal May 29 '14 at 06:54
  • Scale the created bitmap: Bitmap.createScaledBitmap(bitmap, 400, 200, false); to set all the image to be of same widht and height – Audumbar May 29 '14 at 06:56
  • ty it worked but sir now how to rotate the img from the camera??? i tried using exif but no success – Vishal May 29 '14 at 07:35