Search code examples
androidimagebitmaprotationexif

Image gets rotate though the orientation is 0 using exif while selecting from gallery


I am selecting an image from gallery and showing it on a image view. Image is getting selected, but on samsung phones it has issue of rotating the image so to solve that I am checking if the image is rotated or not usif EXIF interface and if rotated change its angle.

But this is not working for some images. Some images I can see straight, but some images if they are straight then also they are getting rotate.

As I did debug the orientation for the image is 0, it goes in the default case and applies normal bitmap to the rotated bitmap. Still the image I see is rotated. Not getting why is this happening..

private void onSelectFromGalleryResult(Intent data) {

    Bitmap bm=null;
    if (data != null) {
        try {
            bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();

    bm = Bitmap.createScaledBitmap(bm,512,512, true);

    bm.compress(Bitmap.CompressFormat.PNG,100, bytes);
    File destination = new File(Environment.getExternalStorageDirectory(),
            System.currentTimeMillis() + ".png");
    FileOutputStream fo;
    try {
        destination.createNewFile();
        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    loadImageFromFile(destination.getAbsolutePath());

}



public void loadImageFromFile(String imageFile){

    try {
        ExifInterface ei = new ExifInterface(imageFile);
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);

            Bitmap bitmap = BitmapFactory.decodeFile(imageFile);

            Bitmap rotatedBitmap = null;
            Log.e("orientation",String.valueOf(orientation)+" check");
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotatedBitmap = rotateImage(bitmap, 90);
                    Log.e("orientation",String.valueOf(orientation)+" check");
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotatedBitmap = rotateImage(bitmap, 180);
                    Log.e("orientation",String.valueOf(orientation)+" check");
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotatedBitmap = rotateImage(bitmap, 270);
                    Log.e("orientation",String.valueOf(orientation)+" check");
                    break;
                case ExifInterface.ORIENTATION_NORMAL:
                    rotatedBitmap = bitmap;
                    Log.e("orientation",String.valueOf(orientation)+" check");
                    break;

                default:
                    rotatedBitmap = bitmap;
                    Log.e("orientation",String.valueOf(orientation)+" check");
                    break;
            }

        if(rotatedBitmap != null)
        {
            profile_image.setImageBitmap(rotatedBitmap);
            selectedBitmap = rotatedBitmap;

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            selectedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); //replace 100 with desired quality percentage.
            byte[] byteArray = stream.toByteArray();

                File tempFile = File.createTempFile("temp",null, getCacheDir());
                FileOutputStream fos = new FileOutputStream(tempFile);
                fos.write(byteArray);

                mProfileImage = tempFile;
        }

    }
    catch (IOException ex) {
      //  UiUtils.showAlert(getString(R.string.error),NewGroupAcvitity.this);
    }
}

public static Bitmap rotateImage(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix,
            true);
}

EDIT:

-

    @SuppressWarnings("deprecation")
-    private void onSelectFromGalleryResult(Intent data) {
-
-        Uri uri = (Uri)data.getData();
-        String[] filePathColumn = { MediaStore.Images.Media.DATA };
-        Cursor cursor = getContentResolver().query(uri,filePathColumn, null, null, null);
-        if(cursor != null) {
-            cursor.moveToFirst();
-            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
-            String picturePath = cursor.getString(columnIndex);
-            cursor.close();
-
-            loadImageFromFile(picturePath);
-        }
-    }

@greenapps - use selected file like this? I tried this, but this was not working in xiaomi devices so I changed the code. Is there any other way for other devices?

What's going wrong here? Can anyone help please? Thank you.


Solution

  • loadImageFromFile(destination.getAbsolutePath());
    

    The image that you try to load is originally from a bitmap which you compressed to jpg and saved to file. destination is the File object for that file.

    Bitmaps do not contain exif information. And hence your jpg file will not contain an exif too.

    So it is useless to use ExifInterface on it.

    Sid i saw this code before. And told the same story. Maybe it was even you which i told it.