43

I had the orientation issue when working with ACTION_IMAGE_CAPTURE activity. I have used the TAG_ORIENTATION so that I would rotate the picture accordingly. But now we found that on some newer devices this doesn't work. In fact it returns 1 for all orientations.

Here's the list of devices we observed this on;

  • Samsung Infuse 4G (2.3.3)
  • Samsung Galaxy SII X (2.3.5)
  • Sony Xperia Arc (2.3.3)

Interesting thing is that once this image is the gallery it shows up properly and if I select it, the TAG_ORIENTATION is populated properly. So somehow the OS fills this information properly but not on ActivityResult.

What's the most reliable way to figure the orientation? Someone on another question suggested comparing height and width but when getting these, they are properly switched based on orientation (another mystery)

EDIT: It seems that this could be connected to another bug where the OS duplicates the image taken in the gallery (it's only supposed to save the image in the URL specified by us), the thing is this image in gallery has the ORIENTATION information while the one in the specified location doesn't.

This is the bug; http://code.google.com/p/android/issues/detail?id=19268

EDIT-2: I've filed a new bug with Android. I'm pretty sure this is an OS bug related the aforementioned bug. http://code.google.com/p/android/issues/detail?id=22822

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
Tolga E
  • 12,188
  • 15
  • 49
  • 61
  • I'm not sure that problem is in the OS itself. Camera an gallery appliactions are developed by phone vendors, and not guaranted to be bug free or behaving correctly. This way you get error on some devices, but not on others. – Konstantin Pribluda Dec 19 '11 at 08:45
  • oh i see what you mean, that is true. Anyways as a solution I decided to build a gallery myself (mostly by copy pasting the android one) – Tolga E Dec 19 '11 at 13:05
  • The motorola xoom also manifests the same problem. – Siddhu Oct 30 '12 at 15:08

5 Answers5

56

Ok guys, it seems like this bug for android won't be fixed for a while. Although I found a way to implement the ExifInformation so that both devices (ones with proper Exif tag, and also improper exif tags work together)..

So the issue is on some (newer) devices, there's a bug that makes the picture taken saved in your app folder without proper exif tags while a properly rotated image is saved in the android default folder (even though it shouldn't be)..

Now what I do is, i record the time when I'm starting the camera app from my app. THen on activity result, I query the Media Provider to see if any pictures were saved after this timestamp I saved. That means that, most likely OS saved the properly rotated picture in the default folder and of course put a entry in the media store and we can use the rotation information from this row. Now to make sure we are looking at the right image, i compare the size of this file to the one I have access to (saved in my own app folder);

    int rotation =-1;
    long fileSize = new File(filePath).length();

    Cursor mediaCursor = content.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] {MediaStore.Images.ImageColumns.ORIENTATION, MediaStore.MediaColumns.SIZE }, MediaStore.MediaColumns.DATE_ADDED + ">=?", new String[]{String.valueOf(captureTime/1000 - 1)}, MediaStore.MediaColumns.DATE_ADDED + " desc");

    if (mediaCursor != null && captureTime != 0 && mediaCursor.getCount() !=0 ) {
        while(mediaCursor.moveToNext()){
            long size = mediaCursor.getLong(1);
            //Extra check to make sure that we are getting the orientation from the proper file
            if(size == fileSize){
                rotation = mediaCursor.getInt(0);
                break;
            }
        }
    }

Now if the rotation at this point is still -1, then that means this is one of the phones with proper rotation information. At this point, we can use the regular exif orientation on the file that's returned to our onActivityResult

    else if(rotation == -1){
        rotation = getExifOrientationAttribute(filePath);
    }

You can easily find out how to find exif orientations like the answer in this question Camera orientation issue in Android

Also note that ExifInterface is only supported after Api level 5.. So if you want to support phones before 2.0, then you can use this handy library I found for java courtesy of Drew Noakes; http://www.drewnoakes.com/code/exif/

Good luck with your image rotating!

EDIT: Because it was asked, the intent I've used and how i started was like this

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//mediaFile is where the image will be saved
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mediaFile));
startActivityForResult(intent, 1);
Community
  • 1
  • 1
Tolga E
  • 12,188
  • 15
  • 49
  • 61
  • good to hear, it took me long time to come up with a viable solution.. Android OS needs to fix this bug – Tolga E Jan 15 '12 at 22:59
  • 1
    yes, true. Actually, even if legitimate bugs (like the one you have identified here) are documented in a common repository, it would save developers a lot of frustration and time. – Abhijit Jan 15 '12 at 23:43
  • well i opened a ticket (link in my question) no responses lol, posted in google groups too – Tolga E Jan 16 '12 at 01:17
  • 3
    @TolgaE "captureTime" is System.currenttimeinMillis() right ? – hacker Nov 09 '12 at 10:29
  • @hacker yep that's what I was using – Tolga E Nov 09 '12 at 14:18
  • Just FYI, this didn't work for me, and it took me a long time to understand that the device timezone was off and that for some reason, that messed everything up. it might be worth using a different method to get the capture time - perhaps nanotime? http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#nanoTime%28%29 – ekatz Jan 31 '13 at 22:30
  • 1
    I don't get this to work on Samsung Galaxy S4 mediaCursor.getCount() is equal to 0 – PaperThick Jan 16 '14 at 12:04
  • when to get the timeStamp ? after starting the intent ? – Jesus Dimrix Sep 03 '14 at 18:29
  • 1
    it does not work , i"m guessing i"m not geting the current time stamp . how to do it ? – Jesus Dimrix Sep 03 '14 at 18:53
  • so what if users takes several photos, and then comes back to your app? are you saving these 'timestamps' for all images taken by your app? – Piotr Sep 08 '14 at 22:50
  • @Piotr, the timestamp is supposed to be taken when you launch the camera intent from your Activity. So it could be messed up, but only if you launch the camera from your Activity and later on you take a normal picture while the camera is still running and also have exactly the same size as your Activity's picture. – Aritz Nov 11 '14 at 10:21
7

you can go this way too:

Matrix matrix = new Matrix();
// rotate the Bitmap (there a problem with exif so we'll query the mediaStore for orientation
Cursor cursor = getApplicationContext().getContentResolver().query(selectedImage,
      new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);
if (cursor.getCount() == 1) {
cursor.moveToFirst();
    int orientation =  cursor.getInt(0);
    matrix.preRotate(orientation);
    }
oferiko
  • 1,927
  • 2
  • 16
  • 17
7

Indeed a problematic bug! I'm not sure I like the suggested workaround, so here's another :)

The key is to use EXTRA_OUTPUT and query it when the image has been captured! Obviously, this only works if you allow yourself to specify the filename.

protected void takePictureSequence() {      
    try {
        ContentValues values = new ContentValues();  
        values.put(MediaStore.Images.Media.TITLE, UUID.randomUUID().toString() + ".jpg");  
        newPhotoUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, newPhotoUri);

        startActivityForResult(intent, ActivityResults.TAKE_NEW_PICTURE_RESULT);
    } catch (Exception e) {
        Toast.makeText(this, R.string.could_not_initalize_camera, Toast.LENGTH_LONG).show();
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == ActivityResults.TAKE_NEW_PICTURE_RESULT) {
        if (resultCode == RESULT_OK) {
            try {
                String[] projection = { MediaStore.Images.Media.DATA }; 
                CursorLoader loader = new CursorLoader(this, newPhotoUri, projection, null, null, null);
                Cursor cursor = loader.loadInBackground();

                int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();

                // Rotation is stored in an EXIF tag, and this tag seems to return 0 for URIs.
                // Hence, we retrieve it using an absolute path instead!
                int rotation = 0;
                String realPath = cursor.getString(column_index_data);
                if (realPath != null) {
                    rotation = ImageHelper.getRotationForImage(realPath);
                }

                // Now we can load the bitmap from the Uri, using the correct rotation.
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public int getRotationForImage(String path) {
    int rotation = 0;

    try {
        ExifInterface exif = new ExifInterface(path);
        rotation = (int)exifOrientationToDegrees(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));
    } catch (IOException e) {
        e.printStackTrace();
    }

    return rotation;
}
l33t
  • 18,692
  • 16
  • 103
  • 180
  • I tried this one on my app and this one worked. The use of the ContentResolver to insert the temporary file into the media store was crucial to make sure that the EXIF data was saved properly. – lazypig Jul 23 '14 at 17:35
  • I have got orientation =6 while taking photo on portrait and getting 1 on landscape .. what does it mean ??? – Ahmad Arslan Feb 10 '15 at 10:07
  • loader.loadInBackground(); return null – Wackaloon Sep 26 '16 at 09:33
2

What I've learned recently is that if you resize the image, it usually loses its EXIF information. So you want to give the new file the old EXIF information.

Source.

Community
  • 1
  • 1
0

My solution for this. Tested on LG G2 mobile. I noticed that when I use camera and take new picture everything works fine. ExifInterface returns right orientation. So it must be something in the path because my path was null in this line of code:

exif = new ExifInterface(path);

but when i used absolute path my app crash. But the solution is in this method below, because it depends on your sdk version. One more note to mention I used absolute path only for selecting gallery picture because if i used it for Camera my app crashed. Im new in programing and just lost 2 days to solve this. Hope it will help someone.

   public String getRealPathFromURI(Uri uri) {
        if(Build.VERSION.SDK_INT >= 19){
            String id = uri.getLastPathSegment().split(":")[1];
            final String[] imageColumns = {MediaStore.Images.Media.DATA };
            final String imageOrderBy = null;
            Uri tempUri = getUri();
            Cursor imageCursor = getContentResolver().query(tempUri, imageColumns,
                    MediaStore.Images.Media._ID + "="+id, null, imageOrderBy);
            if (imageCursor.moveToFirst()) {
                return imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
            }else{
                return null;
            }
        }else{
            String[] projection = { MediaStore.MediaColumns.DATA };
            Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
            if (cursor != null) {
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            } else
                return null;
        }
    }

So I get my ExifInterface in onActivityResult method

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == GALLERY_IMAGE_REQUEST && resultCode == RESULT_OK && data != null) {
        try {
            exif = new ExifInterface(getRealPathFromURI(data.getData()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        showImage(data.getData());
    } else if (requestCode == CAMERA_IMAGE_REQUEST && resultCode == RESULT_OK) {
        try {
            exif = new ExifInterface(Uri.fromFile(getCameraFile()).getPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        showImage(Uri.fromFile(getCameraFile()));
    }
}

and my show image method look like this

public void showImage(Uri uri) {
    if (uri != null) {
        try {

            Bitmap bitmap = scaleBitmapDown(MediaStore.Images.Media.getBitmap(getContentResolver(), uri), IMAGE_SIZE);

            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

            bitmap = rotateBitmap(bitmap, orientation);



            if (whatPlayer.equals("Player1")) {
                mImagePlayer1.setImageBitmap(bitmap);

                bitmapPlayer1 = bitmap; //*save picture in static variable so other activity can use this
            }
            if (whatPlayer.equals("Player2")) {
                mImagePlayer2.setImageBitmap(bitmap);

                bitmapPlayer2 = bitmap;
            }

        } catch (IOException e) {
            Log.d(TAG, "Image picking failed because " + e.getMessage());
            Toast.makeText(this, R.string.image_picker_error, Toast.LENGTH_LONG).show();
        }
    } else {
        Log.d(TAG, "Image picker gave us a null image.");
        Toast.makeText(this, R.string.image_picker_error, Toast.LENGTH_LONG).show();
    }
}
Lantus
  • 35
  • 8