I am getting location from device and trying to write it into exif, the location is getting fine but the data is not writing to the image and also there are no errors, please help.

The code is shown below.

public class MainActivity extends Activity implements LocationListener {

private Uri imPath = null;
private File ff = null;
private TextView ttx = null;

private double longitude = 0;
private double latitude = 0;
private LocationManager lm;
private String provider;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start_main);       
    Button btn = (Button) findViewById(R.id.btn);       
    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);           
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            capture();                              

        }
    });
}   
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO locationListenerGPS onStatusChanged
}
 @Override
public void onProviderEnabled(String provider) {
    Log.w("", "PROVIDOR " + provider + " ENABLED!");
}
 @Override
public void onProviderDisabled(String provider) {
    Log.w("", "PROVIDOR " + provider + " DISABLED!");
}
 @Override
public void onLocationChanged(Location newlocation) {
    longitude = newlocation.getLongitude();
    latitude = newlocation.getLatitude();
    Log.d("","lat: "+latitude+" longi: "+longitude);        
}

public void capture() {
    try {
        ff = new     File(Environment.getExternalStorageDirectory(),String.valueOf(System.currentTimeMillis()) + ".jpg");
        imPath = Uri.fromFile(ff);

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imPath);

        startActivityForResult(intent, 1);

    } catch (ActivityNotFoundException e) {
        e.printStackTrace();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK) {
        switch (requestCode) {
        case 1:
            try{

                    Criteria criteria = new Criteria();
                    provider = lm.getBestProvider(criteria, false);
                    Location location;
                if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

                        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, this);

                            location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            onLocationChanged(location);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                                File f = ff;
                                gTag(f.getAbsolutePath().toString(),location);
                            }

                } else {

                if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {                           
                        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, this);                          
                            location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                            onLocationChanged(location);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                                File f = ff;
                                gTag(f.getAbsolutePath().toString(),location);
                            }

                } else {
                    Toast.makeText(this,
                            "Enable location settings",Toast.LENGTH_LONG).show();

                }
            }      
                }catch(Exception e){
                    e.printStackTrace();
                }
        break;
    }
    }
}

public void gTag(String filename, Location location) {
        ExifInterface exif;
    try {
        exif = new ExifInterface(filename);         
        exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE,     Format.convert(location.getLatitude()));
        exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF,     Format.latitudeRef(location.getLatitude()));
        exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE,     Format.convert(location.getLongitude()));
        exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF,     Format.longitudeRef(location.getLongitude()));
        exif.saveAttributes();
        Toast.makeText(this,"Tagged at lati: "+GPS.convert(location.getLatitude())+" longi:     "+GPS.convert(location.getLongitude()),Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        Log.e("Tag", e.getLocalizedMessage());
    } 

}

}

class Format {
private static StringBuilder sb = new StringBuilder(20);
 //returns ref for latitude which is S or N.

public static String latitudeRef(final double latitude) {
return latitude < 0.0d ? "S" : "N";
}
 //returns ref for latitude which is S or N.

public static String longitudeRef(final double longitude) {
return longitude < 0.0d ? "W" : "E";
}
/*
convert latitude into DMS (degree minute second)
-79.948862 becomes
79/1,56/1,55903/1000    
*/
public static final String convert(double latitude) {
latitude = Math.abs(latitude);
final int degree = (int)latitude;
latitude *= 60;
latitude -= degree * 60.0d;
final int minute = (int)latitude;
latitude *= 60;
latitude -= minute * 60.0d;
final int second = (int)(latitude * 1000.0d);

sb.setLength(0);
sb.append(degree);
sb.append("/1,");
sb.append(minute);
sb.append("/1,");
sb.append(second);
sb.append("/1000");
return sb.toString();
}
}

Related posts

Recent Viewed