使用相机意图后对照片进行地理标记
Posted
技术标签:
【中文标题】使用相机意图后对照片进行地理标记【英文标题】:GeoTagging Photo's after using Camera Intent 【发布时间】:2012-11-27 15:05:01 【问题描述】:我有一个使用 android Camera Intent 拍照的应用程序,在拍照后,我试图将一些 gps 数据添加到图像的 Exif 数据中。但是,当我的应用程序到达double longitude = location.getLongitude();
这是我的代码:
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File folder = new File(Environment.getExternalStorageDirectory() + "/SApp");
if(folder.exists() == false)
folder.mkdirs();
setCamera();
private void setCamera()
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"/SApp/Attachment" + ".jpg")));
startActivityForResult(cameraIntent, 1001);
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if(resultCode == RESULT_OK)
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = (Location) lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
File f = new File(Environment.getExternalStorageDirectory(),"/SApp/Attachment" + ".jpg");
geoTag(f.getAbsolutePath(),latitude,longitude);
public void geoTag(String filename, double latitude, double longitude)
ExifInterface exif;
try
exif = new ExifInterface(filename);
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)
Log.e("PictureActivity", e.getLocalizedMessage());
错误跟踪:
11-27 10:17:25.865: E/AndroidRuntime(27201): FATAL EXCEPTION: main
11-27 10:17:25.865: E/AndroidRuntime(27201): java.lang.RuntimeException: Failure delivering result ResultInfowho=null, request=1001, result=-1, data=null to activity serverbased.app/serverbased.app.ServerAppActivity: java.lang.NullPointerException
11-27 10:17:25.865: E/AndroidRuntime(27201): at android.app.ActivityThread.deliverResults(ActivityThread.java:2994)
11-27 10:17:25.865: E/AndroidRuntime(27201): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3037)
11-27 10:17:25.865: E/AndroidRuntime(27201): at android.app.ActivityThread.access$1100(ActivityThread.java:128)
11-27 10:17:25.865: E/AndroidRuntime(27201): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1191)
11-27 10:17:25.865: E/AndroidRuntime(27201): at android.os.Handler.dispatchMessage(Handler.java:99)
11-27 10:17:25.865: E/AndroidRuntime(27201): at android.os.Looper.loop(Looper.java:137)
11-27 10:17:25.865: E/AndroidRuntime(27201): at android.app.ActivityThread.main(ActivityThread.java:4514)
11-27 10:17:25.865: E/AndroidRuntime(27201): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 10:17:25.865: E/AndroidRuntime(27201): at java.lang.reflect.Method.invoke(Method.java:511)
11-27 10:17:25.865: E/AndroidRuntime(27201): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
11-27 10:17:25.865: E/AndroidRuntime(27201): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
11-27 10:17:25.865: E/AndroidRuntime(27201): at dalvik.system.NativeStart.main(Native Method)
11-27 10:17:25.865: E/AndroidRuntime(27201): Caused by: java.lang.NullPointerException
11-27 10:17:25.865: E/AndroidRuntime(27201): at serverbased.app.ServerAppActivity.onActivityResult(ServerAppActivity.java:139)
11-27 10:17:25.865: E/AndroidRuntime(27201): at android.app.Activity.dispatchActivityResult(Activity.java:4654)
11-27 10:17:25.865: E/AndroidRuntime(27201): at android.app.ActivityThread.deliverResults(ActivityThread.java:2990)
11-27 10:17:25.865: E/AndroidRuntime(27201): ... 11 more
11-27 10:17:39.794: I/Process(27201): Sending signal. PID: 27201 SIG: 9
任何帮助将不胜感激!
【问题讨论】:
崩溃怎么办? NPE?我们可以查看错误日志吗? 是的,它是 NullPointerException 所以location
为空...您应该在调用location.getLongitude()
之前检查该条件。至于为什么它是空的......?
我知道它是空的。我不知道为什么会这样。我不确定它是否会自行打开 GPS,所以我什至打开了 GPS。所以我不确定为什么它为 Null
【参考方案1】:
您检索的位置不正确。我会好好看看这篇博文:A Deep Dive Into Location。它将为您提供许多最佳实践。调用getLastKnownLocation
不会启动位置提供程序。 null
返回值表示没有可用的值。您应该使用 requestLocationUpdates。见下面的 sn-p:
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria fineCriteria.setAccuracy(Criteria.ACCURACY_FINE);
String fineProvider = lm.getBestProvider(fineCriteria, true);
// Start the fine location listener
lm.requestLocationUpdates(fineProvider, 0, 0, locationListenerFine);
private LocationListener locationListenerFine = new LocationListener()
public void onLocationChanged(Location location)
public void onProviderDisabled(String provider)
public void onProviderEnabled(String provider)
public void onStatusChanged(String provider, int status, Bundle extras)
;
【讨论】:
【参考方案2】:问题是我使用 LocationManager.GPS_PROVIDER
将其更改为 LocationManager.NETWORK_PROVIDER
解决了我的问题
【讨论】:
以上是关于使用相机意图后对照片进行地理标记的主要内容,如果未能解决你的问题,请参考以下文章