1
votes

I am sharing a photo from Google+ with my app but I am unable to determine the orientation of the image. That is some images come out with the correct orientation and some are at right angles but I cannot find any metadata that distinguishes between the two. I have tried using the ExifInterface and querying the content resolver without success. I think the ExifInterface approach will only work for images which are local to the device. Querying the ContentResolver only seems to return the name, size and mime-type.

Needless to say GMail and Snapseed handle it perfectly.

Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
ContentResolver resolver = getContentResolver();

System.out.println("image-uri: " + imageUri);
InputStream is = resolver.openInputStream(imageUri);
BitmapFactory.Options dbo = new BitmapFactory.Options();
dbo.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, dbo);
is.close();
System.out.println("got image dims: " + dbo.outWidth + "x" + dbo.outHeight);

ExifInterface exifInterface = new ExifInterface(imageUri.getPath());
int orientation = exifInterface.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
System.out.println("orientation from ExifInterface: " + orientation);

// print out all the metadata we can find
Cursor cursor = resolver.query(imageUri, null, null, null, null);
if (cursor.getCount() == 1) {
  cursor.moveToFirst();
  for (int i = 0; i < cursor.getColumnCount(); i++) {
    System.out.println("col[" + i + "] name:" + cursor.getColumnName(i) + " type:" + cursor.getType(i) + " str:" + cursor.getString(i));
  }
}
cursor.close();

which generates the following output:

  • image-uri: content://com.google.android.apps.photos.content/0/https%3A%2F%2Flh4.googleusercontent.com%2FBzkgMI32pI5sx0LWvQW521i0-KoPn3mDaz1pD8O0MTU5%3Ds0-d
  • got image dims: 40x40
  • can't open '/0/https://lh4.googleusercontent.com/BzkgMI32pI5sx0LWvQW521i0-KoPn3mDaz1pD8O0MTU5=s0-d'
  • orientation from ExifInterface: 0
  • col[0] name:_display_name type:3 str:image.jpg
  • col[1] name:_size type:3 str:2846
  • col[2] name:mime_type type:3 str:image/jpeg

As always any help would be very gratefully received.

try looking at the exif in the incorrect image and compare it with the correct ones the fastest way to do it is to open the photo using notepad - Illegal Argument
I'm not sure where to look for the Exif when the only information I have about the image is a Uri, namely: content://com.google.android.apps.photos.content/0/https%3A%2F%2Flh4.googleusercontent.com%2FBzkgMI32pI5sx0LWvQW521i0-KoPn3mDaz1pD8O0MTU5%3Ds0-d I am relying on a ContentResolver to do all the magic. - acuth