我编写了一个Android应用程序,它将GPS坐标更新为图像文件的exif。
我认为图像文件exif已成功更新,因为读取它会给我保存的GPS坐标。
然而,当我打开文件浏览器以详细查看图像文件时,GPS坐标仍显示未知。如果我重新启动手机,那么exif将显示更新的GPS坐标。编辑:如果我将文件移动到另一个文件夹,信息也将更新。所以我必须在它出现之前对图像文件进行一些特殊的操作。
任何人都知道为什么会如此?我如何解决它,以便它立即更新,而无需转到文件浏览器移动文件或重新启动手机?
提前感谢!
编辑:为两个主要部分添加的代码
主类的代码
public class ShowMapActivity extends MapActivity {
ExifInterface exifInterface;
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
private int Option;
private String selected_img;
float LatLong[] = new float[2];
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.gpsmaploc); // bind the layout to the activity
Bundle extras = getIntent().getExtras();
if(extras !=null) {
selected_img = extras.getString("selected_img");
Option = extras.getInt("Option",0);
}
/*Intent cancelIntent = new Intent(this, A.class);
cancelIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(cncelIntent);*/
// create a map view
RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.mainlayout);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
//mapView.setStreetView(true);
mapController = mapView.getController();
mapController.setZoom(20); // Zoon 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
0, new GeoUpdateHandler());
updateExif(selected_img);
}
更新exif的方法
public void updateExif(String file) {
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
String address = ConvertPointToLocation(point);
String strLongitude = location.convert(location.getLongitude(), location.FORMAT_SECONDS);
String strLatitude = location.convert(location.getLatitude(), location.FORMAT_SECONDS);
String Text = "Lat=" + strLatitude + " Long=" + strLongitude;
String message = String.format(
"Current Location \n%3$s \nLongitude: %1$s \nLatitude: %2$s \n"+Text,
location.getLongitude(), location.getLatitude(),address
);
Toast.makeText(getApplicationContext(), message,
Toast.LENGTH_LONG).show();
try {
exifInterface = new ExifInterface(file);
String LATITUDE = degreeDecimal2ExifFormat(location.getLatitude());
String LATITUDE_REF = "N";
String LONGITUDE = degreeDecimal2ExifFormat(location.getLongitude());
String LONGITUDE_REF = "E";
String message2 = String.format(
"Longitude: "+ LONGITUDE + "\nLatitude: " + LATITUDE );
Toast.makeText(getApplicationContext(), message2,
Toast.LENGTH_LONG).show();
exifInterface.setAttribute(ExifInterface.TAG_GPS_LATITUDE, LATITUDE);
exifInterface.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, LATITUDE_REF);
exifInterface.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, LONGITUDE);
exifInterface.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, LONGITUDE_REF);
exifInterface.saveAttributes();
String exif = "";
float[] LatLong = new float[2];
if(exifInterface.getLatLong(LatLong)){
exif += "\n saved latitude= " + LatLong[0];
exif += "\n saved longitude= " + LatLong[1];
}else{
exif += "Exif tags are not available!";
}
Toast.makeText(getApplicationContext(), exif,
Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 0 :(得分:2)
这段代码似乎解决了我的问题。
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
这将刷新SD卡内容。