Reputation: 715
I am trying to extract Exif data, from an image from gallery, like so:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data.getData() != null) {
Uri uri = data.getData();
String file = uri.getPath();
try {
ExifInterface exif = new ExifInterface(file);
Integer property = exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 9); //is always 0
Log.d("width", property.toString());
String date = exif.getAttribute(ExifInterface.TAG_GPS_DATESTAMP); //is always null
} catch (Exception e) {
e.printStackTrace();
}
}
}
But all the values are either null or 0. What is the correct way of doing it?
Upvotes: 1
Views: 1053
Reputation: 1007584
String file = uri.getPath();
This is useless in most situations. At best, if the Uri
has a file
scheme, the path will be a valid reference to a file on the filesystem, but you may not have the rights to access it. It will be completely useless for any other scheme, and most of the time you will have a scheme of content
.
You have two choices:
Use a different EXIF parser, one that can handle an InputStream
. In that case, you can use ContentResolver
and openInputStream()
to get an InputStream
on the content pointed to by the Uri
. The EXIF code in the Mms app in the AOSP is what I use for this.
Use ContentResolver
and openInputStream()
to get an InputStream
on the content pointed to by the Uri
. Then, use Java I/O to copy those bytes to some file that you control. Then, use your ExifInterface
code with the newly-created file.
Upvotes: 1