如何在Android中从相机中获取拍摄图片的当前位置

时间:2017-08-08 10:03:11

标签: android image location photos

我正在尝试从相机中获取捕获图片的位置。当我尝试使用Exif界面但仍然从照片中返回空值时。请有人帮忙......

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

    }

1 个答案:

答案 0 :(得分:0)

使用内容解析程序使用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'