如何从使用MediaStore.ACTION_IMAGE_CAPTURE保存的照片中获取经度和纬度?

时间:2019-04-29 01:44:21

标签: image geolocation coordinates photo take

在应用中,使用以下代码拍摄图片:

    values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, "DogWalker");
    values.put(MediaStore.Images.Media.DESCRIPTION, "DogWalker");
    imageUri = getActivity().getContentResolver().insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

    //Save the Uri in order to get it back on getPictureResults. In most of the cases this would work
    //without passing the Uri, but when the app is removed from memory imageUri is lost and the app
    //can not recover the picture.
    String stringUri = imageUri.toString();
    SharedPreferences.Editor editor = mContext.getSharedPreferences(Global.MY_PREFS_NAME, 0).edit();
    editor.putString(Global.EXTRA_PICTURE_URI, stringUri);
    editor.apply();

关于活动结果:

    //on ActivityResult method
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    switch (requestCode)
    {
        case takePictureRequestCode:
            getPictureResults(resultCode, intent);
            break;
    }
}

private void getPictureResults(int resultCode, Intent intent) {

    if (resultCode == Activity.RESULT_OK)
    {
        try {
            //Get Saved URI
            Bitmap dogWalkPicture;
            SharedPreferences prefs = mContext.getSharedPreferences(Global.MY_PREFS_NAME, 0);
            String stringUri = prefs.getString(Global.EXTRA_PICTURE_URI, "");
            imageUri = Uri.parse(stringUri);

            String filePath = mGlobal.getRealPathFromURI(mContext, imageUri);
            ExifInterface exif = new ExifInterface(filePath);
            float[] latLng = new float[2];
            boolean hasLatLong = exif.getLatLong(latLng);
            if (hasLatLong) {
                System.out.println("Latitude: " + latLng[0]);
                System.out.println("Longitude: " + latLng[1]);
            }

问题在于hasLatLong总是返回false。没错,图片没有来自latlng的信息,我使用android photos app检查过。当我使用相机拍照(不在应用程序中)时,有latlng数据存在。

有什么主意吗?预先谢谢你。

0 个答案:

没有答案