Selecting photo from new Google Photos app has incorrect orientation

670 Views Asked by At

I'm trying to choose a photo using intents on Android. All works well and photos are being retrieved from any photos app (camera, gallery, screenshots etc.. even if selected from the new Photos app); except for the ones backed up online on Google Photos.

Photos taken in portrait will be retrieved in landscape when getting the Bitmap. I have code to get the orientation of the photo so I can translate accordingly, but the orientation is always 0 when choosing an online photo on the new Google Photos app. Any ideas on how I should get the orientation for these photos as 0 is being returned? Code below - thanks.

Starting intent

Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, IMPORT_PHOTO_RESULT);

Intent result

Uri selectedImageUri = data.getData();
imageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);

//fix rotation
int orientation = -1;
Cursor cursor = MediaStore.Images.Media.query(getContentResolver(), selectedImageUri, new String[] { MediaStore.Images.ImageColumns.ORIENTATION });

try {
    if (cursor.moveToFirst()) {
        orientation = cursor.getInt(0);
    }
} finally {
    cursor.close();
}

imageBitmap = Utils.rotateImageWithRotation(orientation, imageBitmap);

//get stream to display and send
ByteArrayOutputStream stream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
// get byte array here
byte[] bytearray = stream.toByteArray();
sendImage(bytearray, null);

rotate image

public static Bitmap rotateImageWithRotation(int rotationInDegrees, Bitmap mBitmap) {
    Matrix matrix = new Matrix();
    if (rotationInDegrees != 0f) {
        matrix.preRotate(rotationInDegrees);
    }
    Bitmap adjustedBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
    return adjustedBitmap;
}
2

There are 2 best solutions below

0
On BEST ANSWER

I gave up and used this amazing library instead. Kudos for the work!

https://github.com/coomar2841/image-chooser-library

1
On

You can try this:

Bitmap bitmap = null;
            try {
                Bitmap tmpBmp;
                Uri uri = params[0]; // remove uri with uri from intent
                int angle = 0;
                Matrix m = new Matrix();
                BitmapFactory.Options tempOption = new BitmapFactory.Options();
                tempOption.inSampleSize = 2;
                AssetFileDescriptor fileDescriptor = getContentResolver().openAssetFileDescriptor(uri, "r");
                tmpBmp = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, tempOption);

                ExifInterface ei = new ExifInterface(uri.getPath());
                int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

                switch (orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        angle = 90;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        angle = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        angle = 270;
                        break;
                }
                m.postRotate(angle);

                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 2;
                if (Runtime.getRuntime().totalMemory() > 64 * 1024 * 1024) {
                    options.inPreferredConfig = Bitmap.Config.RGB_565;
                }
                bitmap = Bitmap.createBitmap(tmpBmp, 0, 0, tempOption.outWidth, tempOption.outHeight, m, true);
            } catch (Exception e) {
                Log.e(LOG_TAG, e.getMessage(), e);
            }
            return bitmap;