Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8878453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:42:55+00:00 2026-06-14T19:42:55+00:00

When I pick an image from gallery to set it on my imageview in

  • 0

When I pick an image from gallery to set it on my imageview in android application then I see that it comes inverted by 180 or 270 or 90 degress.
So to check/solve this I used EXIF orientations, but it always gives me “0”.

I am not able to get what is the problem.

code:

Uri selectedImageUri = data.getData();

                    absolutePath = selectedImageUri.getPath();

                    exifMedia = new ExifInterface(absolutePath);
                    String exifOrint = exifMedia.getAttribute(ExifInterface.TAG_ORIENTATION);

                    exifOrientation = Integer.parseInt(exifOrint);

                    System.out.println("Orientation Tag is:"+exifOrientation);

                    /** Convert URI into byte */
                    ContentResolver cr = getBaseContext()
                    .getContentResolver();
                    InputStream inputStream = cr
                    .openInputStream(selectedImageUri);
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

                    rotatedBMP = getResizedBitmapImage(
                            bitmap, 100, 100,exifOrientation);

                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    rotatedBMP.compress(Bitmap.CompressFormat.PNG, 100,
                            stream);
                    byteArray = stream.toByteArray();
                    mimProfileImageBrowse.setImageBitmap(rotatedBMP);

method:

public  Bitmap getResizedBitmapImage(Bitmap bm, int newHeight, int newWidth, int exifOrientation) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        /**
         *  create a matrix for the manipulation
         */

        Matrix matrix = new Matrix();
        /**
         *  resize the bit map
         */


        /*

         1 = Horizontal (normal) 
2 = Mirror horizontal 
3 = Rotate 180 
4 = Mirror vertical 
5 = Mirror horizontal and rotate 270 CW 
6 = Rotate 90 CW 
7 = Mirror horizontal and rotate 90 CW 
8 = Rotate 270 CW

         */


        switch (exifOrientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:

            rotate = 270;

            break;


        case ExifInterface.ORIENTATION_ROTATE_180:

            rotate = 180;


            break;

        case ExifInterface.ORIENTATION_ROTATE_90:

            rotate = 90;

            break;

        case ExifInterface.ORIENTATION_TRANSPOSE:

            rotate = 45;

            break;

        default:
            break;
        }


        matrix.postScale(scaleWidth, scaleHeight);
        matrix.postRotate(rotate);

        /**
         * recreate the new Bitmap
         */
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
        resizedBitmap = Bitmap.createScaledBitmap(bm, 65, 65, true);

        return resizedBitmap;

    }

Please help. Also matrix.postROTATE is not rotating the bitmaps. I dont know why.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:42:56+00:00Added an answer on June 14, 2026 at 7:42 pm

    After you get Uri of selected Image call the below functions using this methods,
    Like this,

    Uri selectedImageUri = data.getData();
    Bitmap bitmap = scaleImage(this,selectedImageUri);
    

    The two functions to include in your activity are,

     public static Bitmap scaleImage(Context context, Uri photoUri) throws IOException {
            InputStream is = context.getContentResolver().openInputStream(photoUri);
            BitmapFactory.Options dbo = new BitmapFactory.Options();
            dbo.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(is, null, dbo);
            is.close();
    
            int rotatedWidth, rotatedHeight;
            int orientation = getOrientation(context, photoUri);
    
            if (orientation == 90 || orientation == 270) {
                rotatedWidth = dbo.outHeight;
                rotatedHeight = dbo.outWidth;
            } else {
                rotatedWidth = dbo.outWidth;
                rotatedHeight = dbo.outHeight;
            }
    
            Bitmap srcBitmap;
            is = context.getContentResolver().openInputStream(photoUri);
            if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) {
                float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION);
                float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION);
                float maxRatio = Math.max(widthRatio, heightRatio);
    
                // Create the bitmap from file
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = (int) maxRatio;
                srcBitmap = BitmapFactory.decodeStream(is, null, options);
            } else {
                srcBitmap = BitmapFactory.decodeStream(is);
            }
            is.close();
    
            /*
             * if the orientation is not 0 (or -1, which means we don't know), we
             * have to do a rotation.
             */
            if (orientation > 0) {
                Matrix matrix = new Matrix();
                matrix.postRotate(orientation);
    
                srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(),
                        srcBitmap.getHeight(), matrix, true);
            }
    
            String type = context.getContentResolver().getType(photoUri);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            if (type.equals("image/png")) {
                srcBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
            } else if (type.equals("image/jpg") || type.equals("image/jpeg")) {
                srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            }
            byte[] bMapArray = baos.toByteArray();
            baos.close();
            return BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
        }
    
        public static int getOrientation(Context context, Uri photoUri) {
            /* it's on the external media. */
            Cursor cursor = context.getContentResolver().query(photoUri,
                    new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);
    
            if (cursor.getCount() != 1) {
                return -1;
            }
    
            cursor.moveToFirst();
            return cursor.getInt(0);
        }
    

    To get Uri from File use this :

    public static Uri getImageContentUri(Context context, File imageFile) {
            String filePath = imageFile.getAbsolutePath();
            Cursor cursor = context.getContentResolver().query(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    new String[] { MediaStore.Images.Media._ID },
                    MediaStore.Images.Media.DATA + "=? ",
                    new String[] { filePath }, null);
            if (cursor != null && cursor.moveToFirst()) {
                int id = cursor.getInt(cursor
                        .getColumnIndex(MediaStore.MediaColumns._ID));
                Uri baseUri = Uri.parse("content://media/external/images/media");
                return Uri.withAppendedPath(baseUri, "" + id);
            } else {
                if (imageFile.exists()) {
                    ContentValues values = new ContentValues();
                    values.put(MediaStore.Images.Media.DATA, filePath);
                    return context.getContentResolver().insert(
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                } else {
                    return null;
                }
            }
        }
    

    Q. Can you plz tell me what is “MAX_IMAGE_DIMENSION”? – Dhrupal Mar 13 ’13
    A. its based on your imageview size or the image size what you want to show.. whatever it is 60X60 then 60, 100X100 then 100 @Dhrupal

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My intention is to have the user pick an image from the gallery, then
I am developing an application for iphone that will pick an image from photo
I have button of pick image from android gallery in one activity name DrawingActivity.
I want to pick an image from gallery and then get the path of
I'm developing an application that allows the user to pick an image from the
I am using image picker to pick image from gallery in iPhone device. I
I have used the following code to pick up an image from phone's gallery:
The app that I'm working on allows the user to pick an image from
I am trying to use Android gallery to pick image. Launching gallery is easy
I am using following code to pick image from gallery public void takePhotoFromLibrary() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.