Home > Blockchain >  Getting geographical location of captured image - Android java
Getting geographical location of captured image - Android java

Time:10-29

I'm new to android developement and I'm supposed to use Java as the programming language. I have an app where I'm supposed to be able to capture images and the geographical location of the captured images and display these details. I am displaying the image in an imageView. I have a text file where I'm storing image links as well as the captured images. So, I basically have image links and captured images that are stored in an arraylist then to a text file. Please feel free to ask for anything that I may have missed out in the question.

I tried using EXIFInterface method I found on a Stack Overflow response, I tried using Location provider but to no avail. Maybe where I'm placing the code is incorrect, as I said, I'm new to this. I tried watching YT videos and did some research online and I'm more confused than ever at this point. Another approach I tried using was capturing the current location of the device to an invisible textView then calling it to where the image name is being stored but this did not work either.

The EXIF method I tried: `

        try {

            ExifInterface exifInterface = new ExifInterface(direct); //Direct is the filepath
            Log.d("Latitude", exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE));
            Log.d("Longitude", exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE));
        } catch (IOException e) {
            e.printStackTrace();
        }

`

Location Provider method

@SuppressLint("MissingPermission")
private void showLocation() {

    locationProvider.getLastLocation().addOnSuccessListener(this,
            new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    if (location != null) {
                        hiddenLoc.setText("Current location is: Lat:"   location.getLatitude()
                                  "Lon: "   location.getLongitude());
                     
                    }
                }
            });

}

`

CodePudding user response:

EXIF location is an optional interface- most images won't have one. In fact many (most?) camera apps have stopped using it by default to protect user privacy. You can try it, but don't expect it to be there.

Your location code- lastLocation will return null unless location was already up and running (generally because another app was using it). You'd need to request location updates, rather than rely on lastLocation. Please note that this gets the location of the phone now not the location when a photo was taken. So this only works if you run it when you take the photo (the exif data gets the location where the photo was taken, if its there at all).

  • Related