我的问题是,在某些设备上,从图库中选取后,一些图像在ImageView中旋转。我发现了很多使用ExifInterface来解决这个问题的信息,所以我尝试使用它。但是当我尝试使用图像路径创建ExifInterface实例时,我有一个奇怪的例外。
W/ExifInterface: Invalid image
java.io.IOException: Invalid marker: 89
at android.media.ExifInterface.getJpegAttributes(ExifInterface.java:1598)
at android.media.ExifInterface.loadAttributes(ExifInterface.java:1334)
at android.media.ExifInterface.<init>(ExifInterface.java:1052)
at com.igeotrack.igeo.ui.fragment.ProfileFragment.modifyOrientation(ProfileFragment.java:554)
at com.igeotrack.igeo.ui.fragment.ProfileFragment.setLogo(ProfileFragment.java:515)
at com.igeotrack.igeo.ui.fragment.ProfileFragment.onViewCreated(ProfileFragment.java:186)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1127)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5433)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
我的路径没有错,因为我之前使用它并且它有效。这是我的代码。
private void setLogo() {
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
+ "/Android/data/"
+ getActivity().getPackageName()
+ "/Files");
String mImageName="iGeoTrackUserAvatar.jpg";
File mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
// if (mediaFile.exists()) {
// Glide.with(getActivity())
// .load(mediaFile)
// .transform(new SystemUtils.CircleTransform(getActivity()))
// .diskCacheStrategy(DiskCacheStrategy.NONE)
// .skipMemoryCache(true)
// .into(mUserLogo);
// }
Bitmap bitmap = BitmapFactory.decodeFile(mediaFile.getPath());
try {
bitmap = modifyOrientation(bitmap, mediaFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
mUserLogo.setImageBitmap(bitmap);
}
我从stackoverflow.com上的许多答案中找到了这段代码
public static Bitmap modifyOrientation(Bitmap bitmap, String image_absolute_path) throws IOException {
ExifInterface ei = new ExifInterface(image_absolute_path);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return rotate(bitmap, 90);
case ExifInterface.ORIENTATION_ROTATE_180:
return rotate(bitmap, 180);
case ExifInterface.ORIENTATION_ROTATE_270:
return rotate(bitmap, 270);
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
return flip(bitmap, true, false);
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
return flip(bitmap, false, true);
default:
return bitmap;
}
}
public static Bitmap rotate(Bitmap bitmap, float degrees) {
Matrix matrix = new Matrix();
matrix.postRotate(degrees);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
public static Bitmap flip(Bitmap bitmap, boolean horizontal, boolean vertical) {
Matrix matrix = new Matrix();
matrix.preScale(horizontal ? -1 : 1, vertical ? -1 : 1);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
我没有找到关于这个问题的任何内容,但我发现Jake Wharton的mention可能是Android中的错误。你能帮助我吗?因为我无法理解如何解决这个问题。
答案 0 :(得分:6)
CommonsWare的建议非常好。当我使用Gradle com.android.support:exifinterface:25.1.0
编译时,这个类已经开始工作而不是标准实现。