Search code examples
androidandroid-location

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


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.


Solution

  • 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?