我正在尝试使用相机将位置信息写入通过我的应用程序拍摄的照片并将控制权返回给应用程序。它在模拟器上运行时工作得非常好但是当我在我的三星注入设备上运行它时,不会捕获位置信息。请让我知道我做错了什么。
启动意图的方法
private void takePicutre(){
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, 2);
}
创建用于保存图像的文件Uri
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
创建用于保存图像的文件
private static File getOutputMediaFile(int type){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyApp");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
return null;
}
}
String timeStamp = new
SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
编写EXIF数据
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ExifInterface exif;
double latitude = cls_raw_lat;
double longitude = cls_raw_lon;
try {
exif = new ExifInterface(fileUri.getPath());
int num1Lat = (int)Math.floor(latitude);
int num2Lat = (int)Math.floor((latitude - num1Lat) * 60);
double num3Lat = (latitude - ((double)num1Lat+ ((double)num2Lat/60))) * 3600000;
int num1Lon = (int)Math.floor(longitude);
int num2Lon = (int)Math.floor((longitude - num1Lon) * 60);
double num3Lon = (longitude - ((double)num1Lon+((double)num2Lon/60))) * 3600000;
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE,
num1Lat+"/1,"+num2Lat+"/1,"+num3Lat+"/1000");
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE,
num1Lon+"/1,"+num2Lon+"/1,"+num3Lon+"/1000");
if (latitude > 0) {
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "N");
} else {
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "S");
}
if (longitude > 0) {
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "E");
} else {
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "W");
}
exif.saveAttributes();
} catch (IOException e) {
Toast.makeText(this, "Error: "+e.getMessage(),
Toast.LENGTH_LONG).show();
}
}