我正在尝试在我的应用程序中处理图像。目前我面临的问题与图像的方向有关。从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
的返回值总是 0 ORIENTATION_UNDEFINED
。我不知道我做错了什么。
ExifInterface
对我没有帮助。 - Ammar