从新的Google相册应用中选择照片的方向不正确

时间:2015-06-25 14:11:32

标签: android android-intent android-camera orientation

我试图在Android上使用意图选择照片。一切正常,照片正在从任何照片应用程序(相机,画廊,屏幕截图等)中检索..即使从新的照片应用程序中选择);除了在Google相册上在线备份的那些。

获取位图时,将以横向方式检索以纵向拍摄的照片。我有代码来获取照片的方向,以便我可以相应地进行翻译,但在新的Google相册应用中选择在线照片时,方向始终为0。关于如何将这些照片定位为0的任何想法都将被退回?代码如下 - 谢谢。

开始意图

Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, IMPORT_PHOTO_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);

旋转图片

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 个答案:

答案 0 :(得分:1)

我放弃并使用了这个神奇的图书馆。感谢工作!

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

答案 1 :(得分:0)

你可以试试这个:

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;