我想找出图片的 EXIF 旋转。当图片在图库中显示时,它没有旋转,但是在加载带有 EXIF 信息“向左旋转”的图像后,图片旋转显示。
现在我想问用户,他想使用旋转后的图像还是原始图像。
我得到该方法的 Uri 并将其存储在位图中
InputStream inputStream = PaintroidApplication.applicationContext.getContentResolver().openInputStream(bitmapUri);
BitmapFactory.decodeStream(inputStream, null, options);
inputStream.close();
现在我想使用 ExifInterface 来确定旋转,但是 ExifInterface 需要一个路径:
ExifInterface exif = new ExifInterface(bitmapUri.getPath());
rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
现在我遇到路径问题,logcat 显示以下消息:
E/JHEAD﹕ can't open '/document/image:15035'
我该如何解决这个问题,或者是否有其他解决方案来找出 EXIF 信息?
最佳答案
我遇到过同样的问题。 V19及以上的api提供的pickers可能会给你这样的结果。试试下面的代码,应该可以解决您的问题。
您修改后的代码
String path = FileUtility.getRealPathFromURI(context, Uri.parse(bitmapUri.getPath());
ExifInterface exif = new ExifInterface(path);
rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
FileUtility.java
/**
* Gets the real path from file
* @param context
* @param contentUri
* @return path
*/
public static String getRealPathFromURI(Context context, Uri contentUri) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return getPathForV19AndUp(context, contentUri);
} else {
return getPathForPreV19(context, contentUri);
}
}
/**
* Handles pre V19 uri's
* @param context
* @param contentUri
* @return
*/
public static String getPathForPreV19(Context context, Uri contentUri) {
String res = null;
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
if(cursor.moveToFirst()){;
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
return res;
}
/**
* Handles V19 and up uri's
* @param context
* @param contentUri
* @return path
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public static String getPathForV19AndUp(Context context, Uri contentUri) {
String wholeID = DocumentsContract.getDocumentId(contentUri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().
query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{ id }, null);
String filePath = "";
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}
关于Android:从 URI 获取 EXIF 旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28452591/