How to get Exif data from InputStream, not a file?

The reason I'm asking about this is because the Intent file select callback returns a Uri .

Open file with intent:

 Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Select Picture"), CHOOSE_IMAGE_REQUEST); 

Callback:

 @Override public void onActivityResult(int requestCode, int resultCode, final Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == CHOOSE_IMAGE_REQUEST && resultCode == Activity.RESULT_OK) { if (data == null) { // Error return; } Uri fileUri = data.getData(); InputStream in = getContentResolver().openInputStream(fileUri); // How to determine image orientation through Exif data here? } } 

One way is to write an InputStream to the actual File , but this seems like a bad workaround to me.

+6
source share
1 answer

After implementing the support library 25.1.0, you can now read exif data from the contents of the URI (content: // or file: //) through an InputStream .

Example: First add this line to your gradle file:

compile 'com.android.support:exifinterface:25.1.0'

 Uri uri; // the URI you've received from the other app InputStream in; try { in = getContentResolver().openInputStream(uri); ExifInterface exifInterface = new ExifInterface(in); // Now you can extract any Exif tag you want // Assuming the image is a JPEG or supported raw format } catch (IOException e) { // Handle any errors } finally { if (in != null) { try { in.close(); } catch (IOException ignored) {} } } 

For more information check: Introducing the ExifInterface Support Library , ExifInterface .

+8
source

All Articles