2

How to pick an image from gallery (SD Card) for my app?

I have implemented the second answer (With the downsampling). When I select an image in portrait, the image will be shown in landscape mode.. Does anybody know why this is? And how to fix this? Thanks in advance!

P.s. Sorry I've made a new topic out of this but the poster protected his topic against newbies like me :)

Community
  • 1
  • 1
Maarten Meeusen
  • 482
  • 1
  • 9
  • 23

1 Answers1

2

You have to get the exif rotation of the pic, like this and arrange youur bitmap accordingly

public static int getExifRotation(String imgPath) 
{
    try 
    {
        ExifInterface exif = new ExifInterface(imgPath);
        String rotationAmount = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        if (!TextUtils.isEmpty(rotationAmount)) 
        {
            int rotationParam = Integer.parseInt(rotationAmount);
            switch (rotationParam) 
            {
                case ExifInterface.ORIENTATION_NORMAL:
                    return 0;
                case ExifInterface.ORIENTATION_ROTATE_90:
                    return 90;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    return 180;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    return 270;
                default:
                    return 0;
            }
        } 
        else 
        {
            return 0;
        }
    }
    catch (Exception ex) 
    {
        return 0;
    }
}

get the path of the picture

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

Thin make a matrix and use bitmap constructor that uses matrix

Matrix matrix = new Matrix();
matrix.preRotate(90); 
// or
matrix.postRotate(90);

so inside your onActivityResult you should have something like this

 Uri selectedImageUri = data.getData();

                selectedImagePath = getPath(selectedImageUri);
                orientation = getExifRotation(selectedImagePath);


                Matrix matrix = new Matrix();
                matrix.postRotate(90);
               if(orientation == 90){
                   bitmap = Bitmap.createBitmap(bitmap, 0, 0, 
                            bitmap.getWidth(), bitmap.getHeight(), 
                            matrix, true);}

make sure you resample your image first though, so how he has it in his answer first and then do this

JRowan
  • 6,824
  • 8
  • 40
  • 59
  • Where should I call getExifRotation()? And what would be the string-parameter? And where do I make the matrix? – Maarten Meeusen Jun 06 '13 at 09:53
  • In onActivityResult, you get the path of the file, use that for the exif method and that returns number you check to make matrix, then when you make the bitmap use the constructor that takes the matrix and it will rotate the bitmap, the phone saves some pics taken rotated 90 degrees, so that's why there is exifrotation – JRowan Jun 06 '13 at 18:01
  • I think you misread. I have implemented the second answer... The one from siamii. This solution will transform a URI instead of a String... – Maarten Meeusen Jun 06 '13 at 20:52
  • this method getExifRotation is the reason why you asked why the portrait is in landscape mode, you have to get the String path of the pic selected and use this method to know if the pic is saved tilted on the side so you can rotate it – JRowan Jun 07 '13 at 01:58
  • it appears to be right side up, but the android system saves the byte[] on the side in some cases setting the attribute of ExifInterface – JRowan Jun 07 '13 at 01:59
  • your right, that is to resample the picture to a reasonable resolution, if you look at the first answer it gets the String path, you can also do that before and then exifrotation, better yet i will post – JRowan Jun 07 '13 at 02:02