How to get latitude and longitude from captured image -- Android

12,914

Solution 1

What Monkeyless suggested is right, use the Exifinterface. There is a working example in the accepted answer to here:

How to get the latititude and longitude of an image in sdcard to my application?

Solution 2

The camera may or may not capture location data with the image, that depends on the user's camera app and whatever settings they are using (you can disable geo tagging photos, by default on most android phones it is disabled). If any location data is attached to the image, you can find it either using ExifInterface with the path to the image, or using the MediaStore.Images.Media database using the lat/lng columns.

You can never guarantee that you will always get location data for any photo. Providers (gps/wifi/cell) might be disabled, geo tagging might be disabled, and even if both are enabled, the phone may not be able to acquire a recent enough and accurate enough geo point.

Share:
12,914
Ram
Author by

Ram

Updated on June 14, 2022

Comments

  • Ram
    Ram almost 2 years

    I am writing code to retrieve lat and long from image capture. I can able to take image using camera event and onActivityResult.

    eprotected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Uri _uri = null;
        Cursor cursor = null;
    
        try {
            final int PICK_IMAGE = 1;
            if (requestCode == PICK_IMAGE && data != null
                    && data.getData() != null) {
                _uri = data.getData();
    
                if (_uri != null) {
                    // User had pick an image.
                    cursor = getContentResolver()
                            .query(_uri,
                                    new String[] { android.provider.MediaStore.Images.ImageColumns.DATA },
                                    null, null, null);
                    cursor.moveToFirst();
    
                    // Link to the image
                    final String imageFilePath = cursor.getString(0);
                    // Toast.makeText(getApplicationContext(), imageFilePath,
                    // Toast.LENGTH_LONG).show();
                    imageLocation= imageFilePath;
    
                    File imgFile = new File(imageFilePath);
                    if (imgFile.exists()) {
                        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                        captureImage.setImageBitmap(myBitmap);
    
                    }
                    cursor.close();
                } else {
    
                    // Toast.makeText(getApplicationContext(), getSdCard,
                    // Toast.LENGTH_LONG).show();
                }
            }
            super.onActivityResult(requestCode, resultCode, data);
        } catch (Exception e) {
            if (cursor == null || cursor.equals("")) {
                String getSdCard = _uri.getPath();
                imageLocation= getSdCard;
                File imgFile = new File(getSdCard);
                if (imgFile.exists()) {
    
                    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    
                    captureImage.setImageBitmap(myBitmap);
    
                }
            }
            e.printStackTrace();
        }
    }
    

    From this how come we get the latitude and longitude from the image. i searched a while, i cant able to get the location.