如何在Android中从摄像头获取当前拍摄图片的位置
我正在尝试从摄像头获取拍摄图片的位置。当我尝试的Exif接口,但仍然photos.Please有人帮助返回空值...如何在Android中从摄像头获取当前拍摄图片的位置
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 100);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == RESULT_OK && data != null) {
// Let's read picked image data - its URI
Uri pickedImage = data.getData();
// Let's read picked image path using content resolver
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
img1 = cursor.getString(cursor.getColumnIndex(filePath[0]));
ExifInterface exif = null;
try {
exif = new ExifInterface(img1);
} catch (IOException e) {
e.printStackTrace();
}
String lat = ExifInterface.TAG_GPS_LATITUDE;
String lat_data = exif.getAttribute(lat);
Log.e("MYDATA", lat_data);
cursor.close();
Toast.makeText(this, "Image Uploaded", Toast.LENGTH_SHORT).show();
}
使用内容解析器随同InputStream建立ExifInterface
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode != Activity.RESULT_OK) {
return;
}
final Uri originalUri = intent.getData();
try {
final InputStream inputStream = getContext().getContentResolver().openInputStream(originalUri);
final android.support.media.ExifInterface exif = new android.support.media.ExifInterface(inputStream);
final double[] latLong = exif.getLatLong();
Log.v(LOG_TAG, "Image selected at position: " + latLong[0] + " : " + latLong[1]);
} catch (IOException e) {
Log.w(LOG_TAG, "Error when getting location from image", e);
}
}
支持的lib进口
compile 'com.android.support:exifinterface:x.x.x'
final double [] latLong = exif.getLatLong(????);在这个论据中应用了 –
检查文档的api,方法在不同版本的api中发生了变化。使用支持版本导入android.support.media.ExifInterface; – smora
ExifInterface exif = null; 尝试exif = new ExifInterface(img1); catch(IOException e){ e.printStackTrace(); } String lat = ExifInterface.TAG_GPS_LATITUDE; 字符串lat_data = exif.getAttribute(lat); Log.e(“MYDATA”,lat_data); String log = ExifInterface.TAG_GPS_LONGITUDE; String log_data = exif.getAttribute(log); Log.e(“MYDATA1”,log_data);感谢我使用此代码获得解决方案 –
分享你的代码你试过的东西 – sumit
验证你的文件真的有位置数据,大多数时候android设备的功能在默认情况下禁用相机应用程序。 – smora
其实我在Android设备中启用地理定位。 –