【问题标题】:Android getting orientation from MediaStore UriAndroid 从 MediaStore Uri 获取方向
【发布时间】:2015-10-21 00:29:45
【问题描述】:

我似乎无法从 MediaStore Uri 获得图像的正确方向。从设备库中选择图像后,UriChooser 意图返回。

我已经尝试了我在网上找到的以下代码的所有组合,但似乎没有任何效果。无论图像的实际方向如何,这段代码都将orientation设置为0。

通过检查我设备上的图像详细信息,我知道实际方向不是 0。

public static void copyAndResizePhoto(Context context, Uri srcUri, File destFile) {
    String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
    Cursor cursor = context.getContentResolver().query(srcUri, orientationColumn, null, null, null);
    int orientation = -1;
    if (cursor != null && cursor.moveToFirst()) {
        orientation = cursor.getInt(cursor.getColumnIndex(orientationColumn[0]));
    }
    if (cursor != null)
        cursor.close();
}

感谢任何帮助。谢谢。

【问题讨论】:

    标签: android cursor uri mediastore


    【解决方案1】:

    我最初的解决方案仅适用于从MediaStore 中挑选的图像。以下解决方案适用于 ANY Uri,这意味着无论用户从哪里挑选照片,此方法都会固定其方向。

    以下方法需要两个库:metadata-extractor-2.6.4xmpcore-5.1.2.jar

    /**
     * Rotates the specified bitmap to the correct orientation. Note that this library requires
     * the following two libraries:
     * 1. <a href="https://code.google.com/p/metadata-extractor/downloads/list">metadata-extractor-2.6.4</a>
     * 2. <a href="https://github.com/drewfarris/metadata-extractor/tree/master/Libraries">xmpcore-5.1.2.jar</a>
     *
     * @param uri The Uri to the photo file.
     * @param bmp The Bitmap representative of the photo file.
     * @return A correctly rotated Bitmap.
     */
    private static Bitmap fixOrientation(Uri uri, Bitmap bmp) {
        try {
            InputStream inputStream = mContext.getContentResolver().openInputStream(uri);
            Metadata meta = null;
    
            if (inputStream != null) {
                try {
                    meta = ImageMetadataReader.readMetadata(new BufferedInputStream(inputStream), false);
                } catch (ImageProcessingException e) {
                    e.printStackTrace();
                }
    
                if (meta != null) {
                    int orientation = 0;
                    ExifIFD0Directory exifIFD0Directory = meta.getDirectory(ExifIFD0Directory.class);
    
                    if (exifIFD0Directory.containsTag(ExifIFD0Directory.TAG_ORIENTATION))
                        try {
                            orientation = exifIFD0Directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
                        } catch (MetadataException e){
                            e.printStackTrace();
                        }
    
                    switch (orientation) {
                        case ExifInterface.ORIENTATION_ROTATE_90:
                            bmp = rotateBitmap(bmp, 90);
                            break;
    
                        case ExifInterface.ORIENTATION_ROTATE_180:
                            bmp = rotateBitmap(bmp, 180);
                            break;
    
                        case ExifInterface.ORIENTATION_ROTATE_270:
                            bmp = rotateBitmap(bmp, 270);
                            break;
                    }
                }
            }
    
        } catch (IOException e) {
            Log.d(TAG, "Error opening InputStream from uri: " + uri.toString());
            e.printStackTrace();
        }
    
        return bmp;
    }
    

    享受, 科恩

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-25
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      相关资源
      最近更新 更多