【问题标题】:How to Fetch Current location of Taken Picture from Camera in Android如何在 Android 中从相机中获取拍摄照片的当前位置
【发布时间】:2017-08-08 10:03:11
【问题描述】:

我正在尝试从相机中获取拍摄照片的位置。当我尝试 Exif 接口但仍然从照片返回 null 值时。请有人帮助...

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();

    }

【问题讨论】:

  • 分享你尝试过的代码
  • 验证您的文件是否真的有位置数据,大多数时候安卓设备在相机应用中默认禁用该功能。
  • 实际上我在安卓设备中启用了地理标记。
  • exif.getLatLong(latLong);为我工作,但现在已弃用。

标签: android image location photos


【解决方案1】:

使用内容解析器通过 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);
        }
    } 

支持库导入

compile 'com.android.support:exifinterface:x.x.x'

【讨论】:

  • 查看文档api,不同版本的api方法有变化。使用支持版本 import android.support.media.ExifInterface;
  • ExifInterface exif = null;尝试 { exif = new ExifInterface(img1); } catch (IOException e) { e.printStackTrace(); } 字符串 lat = ExifInterface.TAG_GPS_LATITUDE;字符串 lat_data = exif.getAttribute(lat); Log.e("MYDATA", lat_data);字符串日志 = ExifInterface.TAG_GPS_LONGITUDE;字符串 log_data = exif.getAttribute(log); Log.e("MYDATA1", log_data);感谢我使用此代码获得解决方案
  • 感谢大家重播
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多