我想使用ExifInterface来获取照片的GPS坐标。我无法弄清楚如何格式化文件路径以传递给ExifInterface的构造函数。我尝试传递Uri
,File
类型的对象,我也尝试了Strings
我输入已知文件路径的地方。
我创建了一个调用相机应用的活动,然后显示一个显示刚刚拍摄的照片的表单,并允许用户在某些EditText
中输入一些信息。然后,它使用EditText
(i.putExtra()
为Intent)将i
的信息发送回主要活动。我想发送刚刚拍摄的照片的GPS坐标以及其他信息。
这是我的代码,我在调用相机应用程序之前创建了一个fileUri
来保存图像:
public class AddPicActivity extends FragmentActivity {
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;
private Uri fileUri;
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// create Intent to take a pic & return control to the calling app
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// create a file to save the image
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
// set the image file name
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
// My layout file for this activity is "addflowlayout.xml"
setContentView(R.layout.addflowlayout);
// this is the submit button that's in R.layout.addflowlayout
final Button button = (Button)findViewById(R.id.AddPicSubmitButton);
// the submit button's click listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// gathering up the inputs that the user wrote in title,
// then description.
final EditText titlefield =
(EditText)findViewById(R.id.editText_Title);
String title = titlefield.getText().toString();
final EditText descfield =
(EditText)findViewById(R.id.editText_Description);
String desc = descfield.getText().toString();
// gathering the latitude and longitude that get parsed
// out of the JPEG during the function photoloc():
double x = Double.parseDouble(photoloc(fileUri)[0]); // latitude
double y = Double.parseDouble(photoloc(fileUri)[1]); // longitude
// Creating an intent to go back to MainActivity, which sends back
// the information from the EditText's title & description, plus
// the photo and the photo's latitude and longitude, and a boolean
// to tell MainActivity to acknowledge them all.
Intent i = new Intent(context, MainActivity.class);
i.putExtra("title", title);
i.putExtra("desc", desc);
i.putExtra("fileUri", fileUri);
i.putExtra("lat", x);
i.putExtra("long", y);
i.putExtra("timeToAddMarker", true);
startActivity(i);
}
});
}
// The function photoloc() is a function to take the image and parse out
// the latitude and longitude from the JPEG using ExifInterface.
private String[] photoloc(String name){
String[] locs = new String[2];
// Use "fake" coordinates as defaults, until ExifInterface replaces them.
locs[0]= "40.439338";
locs[1]= "-79.919255";
// Need to implement ExifInterface here using the try/catch.
// Need to figure out how to format the filename also.
ExifInterface info = new ExifInterface(name);
locs[0]=info.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
locs[1]=info.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
return locs;
}
/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted using
// Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new
File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
return null;
}
}
// Create a media file name
String timeStamp =
new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() +
File.separator +"IMG_"+ timeStamp + ".jpg");
System.out.println("The full name of mediaFile is "
+ mediaStorageDir.getPath() + File.separator + "IMG_"
+ timeStamp + ".jpg");
tempname = "MyCameraApp/IMG_"+timeStamp+".jpg";
System.out.println("tempname is "+tempname);
} else {
return null;
}
return mediaFile;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
// shows the filename of the photo that was just taken.
Toast.makeText(this, "Image saved to:\n" + data.getData(),
Toast.LENGTH_LONG).show();
ImageView picview = (ImageView)findViewById(R.id.photo_zone);
// set the just-taken photo as the content of the imageview in
// my layout with the ID "photo_zone"
picview.setImageURI(fileUri);
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}
}