【问题标题】:Selecting photo from new Google Photos app has incorrect orientation从新的 Google 相册应用中选择照片的方向不正确
【发布时间】:2015-06-25 14:11:32
【问题描述】:

我正在尝试使用 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;
}

【问题讨论】:

    标签: android android-intent android-camera orientation


    【解决方案1】:

    我放弃了,转而使用了这个神奇的库。为工作点赞!

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

    【讨论】:

      【解决方案2】:

      你可以试试这个:

      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;
      

      【讨论】:

      • 不走运 - 那些正在工作的最终由于某种原因无法使用这种方法......:/
      猜你喜欢
      • 2015-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-10
      • 2015-12-21
      • 1970-01-01
      • 2022-01-14
      • 2015-10-20
      相关资源
      最近更新 更多