![](/img/trans.png)
[英]Why when loading image with Glide as ByteArray into an ImageView doesn't rotate it according to EXIF?
[英]Using exif to rotate an image
我正在關注此鏈接Android獲取相機位圖的方向? 並在需要時向后旋轉-90度以旋轉我的圖像,但對我不起作用,我收到此錯誤
05-28 23:29:30.049 9735-9735 / ss.sealstudios.com.socialstories E / JHEAD:無法打開'/ document / 508'
但是像下面這樣檢查它看起來像是指向正確的地方
05-28 23:29:30.049 9735-9735 / ss.sealstudios.com.socialstories I / System.out:位圖uri content://com.android.providers.downloads.documents/document/508 0
//最后的0是我檢查旋轉
我想知道這是否是特定於構建版本的,例如從URI獲取真實路徑,因為在我的棉花糖設備上運行此代碼會給我一個完全不同的結果(此結果,即:錯誤代碼來自kitkat設備),但是在無數種無濟於事的答案之后,我擊敗了我幾個星期,任何人都可以稱重並幫助我,這就是我想要做的
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data
!= null
&& data.getData() != null)
{
Uri uri = data.getData();
try {
BitmapFactory.Options bitmapOptions = new
BitmapFactory.Options();
bitmapOptions.inSampleSize = (int) 4;
InputStream inputStream =
getContentResolver().openInputStream(uri);
Bitmap scaledBitmap = BitmapFactory.decodeStream(inputStream,
null, bitmapOptions);
ExifInterface exif = new ExifInterface(uri.getPath());
int rotation =
exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
int rotationInDegrees = exifToDegrees(rotation);
System.out.println("bitmap uri " + uri + " " + rotation);
Matrix matrix = new Matrix();
if (rotation != 0f){
matrix.preRotate(rotationInDegrees);
Bitmap.createBitmap(scaledBitmap, 0,
0,scaledBitmap.getWidth(),scaledBitmap.getHeight(),
matrix, true);
imageView.setImageBitmap(scaledBitmap);
}else
imageView.setImageBitmap(scaledBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static int exifToDegrees(int exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
return 90;
}
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
return 180; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
return 270; }
return 0;
}
非常感謝
ExifInterface exif = new ExifInterface(uri.getPath());
這是行不通的。 如果在Uri
上調用getPath()
,則說明這樣做是錯誤的,因為這對於大多數Uri
值(尤其是具有content
方案的Uri
值)毫無意義。
由於SDK的ExifInterface
僅適用於本地文件,因此您將需要其他EXIF代碼。 我為此使用了AOSP中的一些代碼 。 該版本的ExifInterface
可以與InputStream
一起使用,因此您可以為Uri
標識的內容獲取一個新的InputStream
,並將其傳遞給替代的ExifInterface
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.