0

I'm trying to retrieve longitude and latitude values from geotagged images using android.media.ExifInterface library. However, getLatLong always returns false although the image is geotagged. This is my code :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button galleryButton = (Button) findViewById(R.id.galleryButton);

    galleryButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(pickPhoto , 1);


        }

    });


}


protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {

    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    switch(requestCode) {
        case 0:
            if(resultCode == RESULT_OK){
                Uri selectedImage = imageReturnedIntent.getData();
                ExifInterface exif = new ExInterface(selectedImage.getPath());
                float[] latlong = null;
                bool result = exif.getLatLong(latlong);


            }

            break;

    }
}

As you can see, I'm initializing exif object with the path from returned URI and it contains path like "/external/images/media/2330". When I call getLatLong, it always returns false. I have also tried the example from this page, but still does not work. Any help would be appreciated.

Galaxy S6 Edge Android SDK Version 25

KMC
  • 1,677
  • 3
  • 26
  • 55

1 Answers1

1

getPath() only works on a Uri with a file scheme. Yours has a content scheme. Plus, you are using the ExifInterface with the security flaws.

Add the com.android.support:exifinterface:25.3.1 library to your dependencies.

Next, replace your android.media.ExifInterface import with one for android.support.media.ExifInterface.

Then, replace:

ExifInterface exif = new ExInterface(selectedImage.getPath());

with:

ExifInterface exif = new ExifInterface(getContentResolver().openInputStream(selectedImage));
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491