Search code examples
androidandroid-imageviewandroid-file

Is this bug in ExifInterface?


My problem is that on some devices some images after picking from gallery became rotated in ImageView. I found a lot of information of using ExifInterface for solving this problem, so I tried to use it. But I have strange exception when I try to create ExifInterface instance using path of image.

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)

My path is not wrong, because I used it before and it worked. Here is my code.

        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);
    }

This code I found from many answers on 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);
}

I didn't find anything about this problem, but I found Jake Wharton's mention that it is probably bug in Android. Can you help me? Because I can't understand how to solve this.


Solution

  • CommonsWare's advice was really good. When I compiled using Gradle com.android.support:exifinterface:25.1.0 this class has started working instead of standard implementation.