Android从MediaStore Uri获取方向信息
我似乎无法从MediaStoreAndroid从MediaStore Uri获取方向信息,android,cursor,uri,mediastore,Android,Cursor,Uri,Mediastore,我似乎无法从MediaStoreUri获取图像的正确方向。从设备库中选择图像后,从选择器意图返回Uri 我已经尝试了我在网上找到的以下代码的每一种组合,但似乎没有任何效果。无论图像的实际方向如何,此代码都将方向设置为0 通过检查设备上的图像详细信息,我知道实际方向不是0 public static void copyAndResizePhoto(Context context, Uri srcUri, File destFile) { String[] orientationColumn
Uri
获取图像的正确方向。从设备库中选择图像后,从选择器
意图返回Uri
我已经尝试了我在网上找到的以下代码的每一种组合,但似乎没有任何效果。无论图像的实际方向如何,此代码都将方向设置为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();
}
感谢您的帮助。谢谢。我的原始解决方案仅适用于从MediaStore
中挑选的图像。以下解决方案适用于任何Uri
,这意味着无论用户从何处选择照片,此方法都将固定其方向
以下方法需要两个库:和
/**
*将指定的位图旋转到正确的方向。请注意,此库需要
*以下两个库:
* 1.
* 2.
*
*@param uri照片文件的uri。
*@param bmp代表照片文件的位图。
*@返回正确旋转的位图。
*/
专用静态位图固定方向(Uri、位图bmp){
试一试{
InputStream InputStream=mContext.getContentResolver().openInputStream(uri);
元数据meta=null;
如果(inputStream!=null){
试一试{
meta=ImageMetadataReader.readMetadata(新的BufferedInputStream(inputStream)),false;
}捕获(ImageProcessingException e){
e、 printStackTrace();
}
if(meta!=null){
int方向=0;
exiffd0directory exiffd0directory=meta.getDirectory(exiffd0directory.class);
if(exiffd0directory.containsTag(exiffd0directory.TAG_方向))
试一试{
方向=exiffd0directory.getInt(exiffd0directory.TAG\u方向);
}捕获(元数据异常e){
e、 printStackTrace();
}
开关(方向){
外壳出口接口。方向旋转90:
bmp=旋转坐标图(bmp,90);
打破
外壳出口接口.方向旋转180:
bmp=旋转体位图(bmp,180);
打破
外壳出口接口方向旋转270:
bmp=旋转体位图(bmp,270);
打破
}
}
}
}捕获(IOE异常){
d(标记,“从uri打开InputStream时出错:“+uri.toString()”);
e、 printStackTrace();
}
返回bmp;
}
享受,
科恩我的原始解决方案仅适用于从MediaStore
中拾取的图像。以下解决方案适用于任何Uri
,这意味着无论用户从何处选择照片,此方法都将固定其方向
以下方法需要两个库:和
/**
*将指定的位图旋转到正确的方向。请注意,此库需要
*以下两个库:
* 1.
* 2.
*
*@param uri照片文件的uri。
*@param bmp代表照片文件的位图。
*@返回正确旋转的位图。
*/
专用静态位图固定方向(Uri、位图bmp){
试一试{
InputStream InputStream=mContext.getContentResolver().openInputStream(uri);
元数据meta=null;
如果(inputStream!=null){
试一试{
meta=ImageMetadataReader.readMetadata(新的BufferedInputStream(inputStream)),false;
}捕获(ImageProcessingException e){
e、 printStackTrace();
}
if(meta!=null){
int方向=0;
exiffd0directory exiffd0directory=meta.getDirectory(exiffd0directory.class);
if(exiffd0directory.containsTag(exiffd0directory.TAG_方向))
试一试{
方向=exiffd0directory.getInt(exiffd0directory.TAG\u方向);
}捕获(元数据异常e){
e、 printStackTrace();
}
开关(方向){
外壳出口接口。方向旋转90:
bmp=旋转坐标图(bmp,90);
打破
外壳出口接口.方向旋转180:
bmp=旋转体位图(bmp,180);
打破
外壳出口接口方向旋转270:
bmp=旋转体位图(bmp,270);
打破
}
}
}
}捕获(IOE异常){
d(标记,“从uri打开InputStream时出错:“+uri.toString()”);
e、 printStackTrace();
}
返回bmp;
}
享受,
科恩
/**
* 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;
}