我尝试在Android中旋转位图并在ImageView中显示定向位图。
focus
并使用https://gist.github.com/9re/1990019中的ExifUtil尝试旋转它
uri = Uri.parse(path)
try {
Bitmap myBitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
Bitmap orientedBitmap = ExifUtil.rotateBitmap(path, myBitmap);
imageView.setImageBitmap(orientedBitmap);
} catch (IOException e) {
e.printStackTrace();
}
它给了我这个日志
public class ExifUtil {
public static Bitmap rotateBitmap(String src, Bitmap bitmap) {
try {
int orientation = getExifOrientation(src);
if (orientation == ExifInterface.ORIENTATION_NORMAL) {
return bitmap;
}
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-90);
break;
default:
return bitmap;
}
try {
Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return oriented;
} catch (OutOfMemoryError e) {
e.printStackTrace();
return bitmap;
}
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
private static int getExifOrientation(String src) throws IOException {
int orientation = ExifInterface.ORIENTATION_UNDEFINED;
Log.e("src", src);
try {
ExifInterface exif = new ExifInterface(src);
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, orientation);
} catch (SecurityException | IllegalArgumentException e) {
e.printStackTrace();
}
return orientation;
}
}
为什么ExifInterface exif = new ExifInterface(src);`在带有两个缺失斜杠的文件路径上抛出FileNotFoundException?
答案 0 :(得分:0)
从该路径中删除“file://”,以便最终获得真实的文件系统路径
/storage/emulated/0/DCIM/image20171117_170310.jpg