我正在尝试在我的应用程序中处理图像。我目前面临的问题与图像的方向有关。从 Android 的相机文件夹中选择的图像的缩略图显示为 90 度旋转。我得到的缩略图如下;
Uri thumbUri = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, uri.getLastPathSegment());
if (thumbUri != null){
try {
List<String> parts = uri.getPathSegments();
String lastPart = parts.get(parts.size() - 1);
int index = lastPart.indexOf(":");
if (index != -1)
{
lastPart = lastPart.substring(index+1);
}
long id = Long.parseLong(lastPart);
// get a thumbnail for the image
Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(
context.getContentResolver(),
id,
MediaStore.Images.Thumbnails.MINI_KIND,
null
);
if (bitmap != null)
{
return bitmap;
}
}
catch (Exception e)
{
Log.e(LOG_TAG, "Unable to generate thumbnail from thumbnail uri " + e.getMessage(), e);
}
}
还尝试通过从 ExifInterface
中读取方向来修复它;
ExifInterface ei = new ExifInterface(uri.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
但是返回的orientation
总是0ORIENTATION_UNDEFINED
。无法理解我在这里做错了什么。
最佳答案
得到解决方案。我使用了来自 BitmapUtil
的 getThumbnail(ContentResolver contentResolver, long id)
方法它从游标中读取图像的元数据,然后进行相应的处理。
感谢Jason Fry
对于这个有用的实用程序。
关于android - 相机文件夹图像出现旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27755040/