-3

I want to capture image by using camera app and after that i want to display the location where the image was taken ...please help on this..Im trying with Location listner,ExifInterface but i did'nt get

  • http://stackoverflow.com/questions/15403797/how-to-get-the-latititude-and-longitude-of-an-image-in-sdcard-to-my-application – Parag Chauhan Feb 04 '16 at 07:25

3 Answers3

1

You can use this method by passing your bitmap & text as parameters:

private Bitmap ProcessingBitmap(Bitmap bm1, String captionString){
Bitmap bm1 = null;
Bitmap newBitmap = null;

try {


Config config = bm1.getConfig();
if(config == null){
config = Bitmap.Config.ARGB_8888;
}

newBitmap = Bitmap.createBitmap(bm1.getWidth(), bm1.getHeight(), config);
Canvas newCanvas = new Canvas(newBitmap);

newCanvas.drawBitmap(bm1, 0, 0, null);


if(captionString != null){

Paint paintText = new Paint(Paint.ANTI_ALIAS_FLAG);
paintText.setColor(Color.BLUE);
paintText.setTextSize(50);
paintText.setStyle(Style.FILL);
paintText.setShadowLayer(10f, 10f, 10f, Color.BLACK);

Rect rectText = new Rect();
paintText.getTextBounds(captionString, 0, captionString.length(), rectText);

newCanvas.drawText(captionString, 
  0, rectText.height(), paintText);

Toast.makeText(getApplicationContext(), 
  "drawText: " + captionString, 
  Toast.LENGTH_LONG).show();

}else{
Toast.makeText(getApplicationContext(), 
  "caption empty!", 
  Toast.LENGTH_LONG).show();
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return newBitmap;
}
  • This worked for me. I just wanna do little enhancement in it. I want to add white text with transparent black background. How to do that?? – Suraj Apr 19 '19 at 12:56
0

First,

add the permission:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Then, use the LocationManager:

LocationManager manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location loc = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longi = loc.getLongitude();
double lat = loc.getLatitude();

Use this code into your LocationListener

Then, when you save your image, save also the latitude and the longitude in a SQLite database for example:

table: [my_images] : (id, path, latitude, longitude)

Hope it helps.

Nicolas Cortell
  • 659
  • 4
  • 16
0

You can then add the location data to the image using the ExifInterface class.

private LocationManager locationManager;
private LocationListener locationListener;

// Set up location listener
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        // Update image with location metadata
        try {
            ExifInterface exifInterface = new ExifInterface(imageFilePath);
            exifInterface.setAttribute(ExifInterface.TAG_GPS_LATITUDE, convert(location.getLatitude()));
            exifInterface.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, location.getLatitude() > 0 ? "N" : "S");
            exifInterface.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, convert(location.getLongitude()));
            exifInterface.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, location.getLongitude() > 0 ? "E" : "W");
            exifInterface.saveAttributes();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    // Convert latitude/longitude to exif format
    private String convert(double coord) {
        coord = Math.abs(coord);
        int degrees = (int) coord;
        coord = (coord - degrees) * 60;
        int minutes = (int) coord;
        coord = (coord - minutes) * 60;
        int seconds = (int) (coord * 1000);
        return degrees + "/1," + minutes + "/1," + seconds + "/1000";
    }
};

// Register for location updates
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

//To unregister the location listener after the metadata has been added. You can do this using the following code:
//To remove the location listener and stop updates from the GPS provider

locationManager.removeUpdates(locationListener);
harsh bangari
  • 417
  • 6
  • 19