Skip to content Skip to sidebar Skip to footer

Image From Gallery Rotates Automatically - Android

In my android application i am loading image from device gallery.In that, i am facing issue regarding image orientation. When i load large resolution images from gallery, they are

Solution 1:

Make one class named ExifUtils

publicclassExifUtils {
/**
 * @see http://sylvana.net/jpegcrop/exif_orientation.html
 */publicstatic Bitmap rotateBitmap(String src, Bitmap bitmap) {
    try {
        intorientation= getExifOrientation(src);

        if (orientation == 1) {
            return bitmap;
        }

        Matrixmatrix=newMatrix();
        switch (orientation) {
        case2:
            matrix.setScale(-1, 1);
            break;
        case3:
            matrix.setRotate(180);
            break;
        case4:
            matrix.setRotate(180);
            matrix.postScale(-1, 1);
            break;
        case5:
            matrix.setRotate(90);
            matrix.postScale(-1, 1);
            break;
        case6:
            matrix.setRotate(90);
            break;
        case7:
            matrix.setRotate(-90);
            matrix.postScale(-1, 1);
            break;
        case8:
            matrix.setRotate(-90);
            break;
        default:
            return bitmap;
        }

        try {
            Bitmaporiented= 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;
}

privatestaticintgetExifOrientation(String src)throws IOException {
    intorientation=1;

    try {
        /**
         * if your are targeting only api level >= 5 ExifInterface exif =
         * new ExifInterface(src); orientation =
         * exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
         */if (Build.VERSION.SDK_INT >= 5) {
            Class<?> exifClass = Class
                    .forName("android.media.ExifInterface");
            Constructor<?> exifConstructor = exifClass
                    .getConstructor(newClass[] { String.class });
            ObjectexifInstance= exifConstructor
                    .newInstance(newObject[] { src });
            MethodgetAttributeInt= exifClass.getMethod("getAttributeInt",
                    newClass[] { String.class, int.class });
            FieldtagOrientationField= exifClass
                    .getField("TAG_ORIENTATION");
            StringtagOrientation= (String) tagOrientationField.get(null);
            orientation = (Integer) getAttributeInt.invoke(exifInstance,
                    newObject[] { tagOrientation, 1 });
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }

    return orientation;
}

Now you can call it in your Activity by:

  ExifUtils.rotateBitmap("your Image path here", "your bitmap object here");

EDIT:

publicvoiddecodeFile(String filePath) {

    // Decode image size
    BitmapFactory.Optionso=newBitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale tofinalintREQUIRED_SIZE=1024;

    // Find the correct scale value. It should be the power of 2.intwidth_tmp= o.outWidth, height_tmp = o.outHeight;
    intscale=1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Optionso2=newBitmapFactory.Options();
    o2.inSampleSize = scale;
    Bitmapb1= BitmapFactory.decodeFile(filePath, o2);
    Bitmap b= ExifUtils.rotateBitmap(filePath, b1);

    // image.setImageBitmap(bitmap);
}

now call this method

decodeFile(imagepath);

Thanks!

Solution 2:

This works well;

publicclassExifUtils {

publicstatic Bitmap getRotatedBitmap(Context context, Uri uri) {
    try {
        Bitmapbitmap= MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
        floatwidth= (float) bitmap.getWidth();
        floatheight= (float) bitmap.getHeight();
        floatmax= Math.max(width / 1280.0f, height / 1280.0f);
        if (max > 1.0f) {
            bitmap = Bitmap.createScaledBitmap(bitmap, (int) (width / max), (int) (height / max), false);
        }
        BitmaprotateBitmap= rotateBitmap(bitmap,
                newExifInterface(context.getContentResolver().openInputStream(uri))
                        .getAttributeInt(ExifInterface.TAG_ORIENTATION, 1));
        if (rotateBitmap != bitmap) {
            bitmap.recycle();
        }
        return rotateBitmap;
    } catch (Exception e) {
        e.printStackTrace();
        returnnull;
    }
}

privatestatic Bitmap rotateBitmap(Bitmap bitmap, int i) {
    Matrixmatrix=newMatrix();
    switch (i) {
        case2:
            matrix.setScale(-1.0f, 1.0f);
            break;
        case3:
            matrix.setRotate(180.0f);
            break;
        case4:
            matrix.setRotate(180.0f);
            matrix.postScale(-1.0f, 1.0f);
            break;
        case5:
            matrix.setRotate(90.0f);
            matrix.postScale(-1.0f, 1.0f);
            break;
        case6:
            matrix.setRotate(90.0f);
            break;
        case7:
            matrix.setRotate(-90.0f);
            matrix.postScale(-1.0f, 1.0f);
            break;
        case8:
            matrix.setRotate(-90.0f);
            break;
        default:
            return bitmap;
    }
    try {
        BitmapcreateBitmap= Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
        bitmap.recycle();
        return createBitmap;
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        returnnull;
    }
  }
}

Post a Comment for "Image From Gallery Rotates Automatically - Android"