2

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.

Sid
  • 2,792
  • 9
  • 55
  • 111
  • where does "bitmap" in "rotatedBitmap = rotateImage(bitmap, 90);" come from? – Bö macht Blau Nov 10 '16 at 09:11
  • The code does not work for xiaomi. Ok. But does it work for samsung? You are unclear. – greenapps Nov 10 '16 at 09:50
  • works on samsung I can see straight image. But in xiaomi the cursor returns null so I can't select the image. @greenapps – Sid Nov 10 '16 at 10:10
  • As had been said before: You should use a different `ExifInterface` class which can read the file contens from an input stream for the file. `Uri uri = (Uri)data.getData();`. Hence you could use `InputStream is = getContentResolver().openInputStream(uri);` Read stackoverflow as CommonsWare has told before which class you can use. – greenapps Nov 10 '16 at 10:30
  • I did not get it. Can you please show an example? @greenapps – Sid Nov 10 '16 at 10:49
  • No. As i do not work with that other ExifInterface class. You better google for it and read @CommonsWare blog about it. – greenapps Nov 10 '16 at 15:09

1 Answers1

2
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.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • I dont know, I have posted the question todday. but same I am doing for camera and its working for it. Then how to solve this issue for samsung devices. @greenapps – Sid Nov 10 '16 at 09:24
  • The posted code cannot work to begin with. Not on a single device. Because bitmaps do not contain exif info. – greenapps Nov 10 '16 at 09:26
  • If you want to determine the orientation with exif info from a jpg file selected with the gallery app then use that file djrectly. Do not convert the file to bitmap first and then resave to a different file without exif info. – greenapps Nov 10 '16 at 09:28
  • If you need a bitmap to display in an imageview and you want to rotate that bitmap and need exif info then use the original file to extract exif info from. Not from a file which you created from that bitmap. – greenapps Nov 10 '16 at 09:32
  • 1
    `I dont know, I have posted the question todday.`. Well we could call that a lie. http://stackoverflow.com/questions/40172441/rotate-image-taken-from-camera-or-gallery You are struggling so long already and do have a habit of not answering questions and leaving the scene instead. – greenapps Nov 10 '16 at 09:55
  • It was solved that time, again I got problem for xiaomi devices, while trying to solve that issue again I came to this error. :-( @greenapps – Sid Nov 10 '16 at 10:14
  • thank you this worked for this error but yet to test on xiaomi device, may be any other way to select images for xiaomi devices? @greenapps – Sid Nov 10 '16 at 10:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127790/discussion-between-sid-and-greenapps). – Sid Nov 10 '16 at 10:37