0

I have an app in which I visualize metadata of some photos that I've taken and I can edit the location using a Place Picker and update the exif data using the following code:

                ExifInterface exifInterface = null;
                try {
                    exifInterface = new ExifInterface(imageFile.getAbsolutePath());
                } catch (IOException e) {
                    e.printStackTrace();
                }

                if(exifInterface != null) {
                    try {

                        exifInterface.setLatLong(place.getLatLng().latitude,place.getLatLng().longitude);
                        exifInterface.saveAttributes();
                        sendBroadcastToUpdateMediaFile();
                        Log.v(TAG,"Updating with: "+place.getName());

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

The data does get the update because if I close the app and open it again and visualize the Exif data I can see the new location being there, but if I go inside the gallery and click details on the same image the location is not there. The photo format is jpeg/jpg, as only those types can have exif data(I get an error when trying to add exifdata on png for example).

Why is the exif data from the same image inside the gallery not get updated? What am I doing wrong?

Edit:

Solution:

private void sendBroadcastToUpdateMediaFile(){
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(imageFile.getAbsolutePath());
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
    }
Bogdan Daniel
  • 2,689
  • 11
  • 43
  • 76
  • Try using `MediaScannerConnection` and its `scanFile()` method to tell the `MediaStore` about the modified file, and see if that helps. – CommonsWare Jan 09 '18 at 17:48
  • That indeed might be the case. I just checked if restarting the device would update the info, and it does. I will try using a broadcast. – Bogdan Daniel Jan 09 '18 at 17:54
  • @CommonsWare I edited with the answer. Thank you. – Bogdan Daniel Jan 09 '18 at 18:12
  • I suggest that you answer your own question, rather than put the answer in the question itself. As it stands, it looks like this question has no answer. But I'm glad it is now working for you! – CommonsWare Jan 09 '18 at 19:01

1 Answers1

0

In order for the MediaStore to get notified of the update you've just made on a photo, you have to use the MediaScanner to send a broadcast and scan the photo.

private void sendBroadcastToUpdateMediaFile(){
            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            File f = new File(imageFile.getAbsolutePath());
            Uri contentUri = Uri.fromFile(f);
            mediaScanIntent.setData(contentUri);
            this.sendBroadcast(mediaScanIntent);
        }
Bogdan Daniel
  • 2,689
  • 11
  • 43
  • 76