Muhammad Ahmed AbuTalib
Muhammad Ahmed AbuTalib

Reputation: 4292

query file uri with content resolver?

I have the following simple code that I use to investigate the orientation of an image .

Cursor cursor = getContentResolver().query(photoUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

When the photoUri is a content uri i.e. starts from content:// the cursor is retrieved fine. I get a contentUri when i select an image from the gallery.

However when the photoUri is a fileUri i.e. starts from file://a/b/c.jpg (which happens when i capture an image from the camera) the cursor is null .

What gives ? I am really confused on the situation .

Upvotes: 0

Views: 751

Answers (1)

Budius
Budius

Reputation: 39836

answering to the comment:

You can't. MediaStore.Images.ImageColumns.ORIENTATION is a database column stored in the content:// database. The file:// database does not contain such a column, so you can't query it.

answering the question:

To extract orientation, android have the native ExifInterface class: https://developer.android.com/reference/android/media/ExifInterface.html

ExifInterface exif = new ExifInterface(filePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

Upvotes: 1

Related Questions