简体   繁体   中英

Retrieve EXIF data from gallery image in onActivityResult

Currently I am getting an image back from the gallery and populating an imageField with it. I am attempting to pull location information from the image, assuming it exists, but all I keep getting is null values.

I know the image I am testing with has entries for latitude and longitude in its EXIF data.

I've tried everything I can find online and nothing is working. I believe the problem lies in the improper parameter being sent to the ExifInterface. Here's what I've got right now:

...

else if (requestCode == REQUEST_IMAGE_FROM_GALLERY) {
        try {
            Uri selectedImage = data.getData();
            String currentImage = selectedImage.getPath()
            + File.separator + getFileName(selectedImage);
....

Then initialize the ExifInterface:

....

try {
    ExifInterface ei = new ExifInterface(currentImage);
    imageLatitude = ei.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
    imageLongitude = ei.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
....

But imageLatitude and imageLongitude are returning null. I've even gone so far as to replace the parameter sent into ExifInterface as the full path to the image ( /storage/emulated/0/DCIM/Camera/IMG_20150119_170010.jpg ) but still getting null values.

What exactly is supposed to be passed into ExifInterface and, if I'm passing in the correct parameter, why am I getting null values?

I ended up getting it to work using this method:

private String getRealPathFromURI(Uri contentURI, Activity activity) {
    Cursor cursor = activity.getContentResolver()
                    .query(contentURI, null, null, null, null);
    if (cursor == null) { // Source is Dropbox or other similar local file
        // path
        return contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        return cursor.getString(idx);
    }
}

See it in use here:

if (requestCode == REQUEST_IMAGE_FROM_GALLERY) {
        try {
            Uri selectedImage = data.getData();
            String currentImageFile = getRealPathFromURI(selectedImage, this);

            ExifInterface ei = new ExifInterface(currentImageFile);

This provided a proper ExifInterface that allowed me to pull the desired data from the image (where it existed).

Thanks for the input everyone.

Maybe the image path you passed to ExifInterface is invalid, you can check it by using the code:

File file = new File(currentImage); 
if(file.exists()){ 
// print file exist
}else{
 // print file not exist.
}

If it's the problem, you can solve it by pass a valid image path. If it's not the problem you can debug your code and see the attributes in the ExifInterface ei object by adding a breakpoint before the following code:

imageLatitude = ei.getAttribute(ExifInterface.TAG_GPS_LATITUDE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM