微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从 Android Studio 的 Google 相册中的云图像中提取 GPS 元数据?

如何解决如何从 Android Studio 的 Google 相册中的云图像中提取 GPS 元数据?

我正在开发一个 Android 应用程序,其中一项功能是您可以在地图上放置一个标记,其中包含相机胶卷中图像的位置。如果我选择本地存储在设备上的图像,它可以完美运行,但是从 Google 相册中选择不在我手机上的照片无法检索 GPS 数据。我有一个循环来打印元数据的所有目录,但它没有显示出来。我正在使用 https://drewnoakes.com/code/exif/提取元数据。

这是我目前所拥有的:

private void chooseImage() {
    Intent chooseIntent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    chooseIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivityForResult(chooseIntent,PICK_IMAGE_REQUEST);
}
@RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data) {
        super.onActivityResult(requestCode,resultCode,data);
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            filePath = data.getData();
            try {
                Metadata Metadata = ImageMetadataReader.readMetadata(getContentResolver().openInputStream(filePath));

                GpsDirectory gpsDirectory = Metadata.getFirstDirectoryOfType(GpsDirectory.class);
                GeoLocation location = gpsDirectory.getGeoLocation();
                if (location != null) {
                    double lat = location.getLatitude();
                    double lng = location.getLongitude();

                    if (marker != null && !marker.getPosition().equals(new LatLng(lat,lng))) {
                        FragmentManager fm = getSupportFragmentManager();
                        OverwritePositionDialog overwritePositionDialog = OverwritePositionDialog.newInstance(-1,new LatLng(lat,lng));
                        overwritePositionDialog.show(fm,"overwrite");
                    } else {
                        final MarkerOptions options = new MarkerOptions().position(new LatLng(lat,lng)).title("New Spot").snippet(username);
                        mMap.addMarker(options);
                        moveCamera(new LatLng(lat,lng));
                        confirmPosition(true);
                    }
                } else {
                    Glide.with(context).asBitmap().load(filePath).into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(@NonNull Bitmap resource,@Nullable Transition<? super Bitmap> transition) {
                            Bitmap resizedBitmap = getResizedBitmap(resource,resource.getWidth() / 3,resource.getHeight() / 3);
                            Glide.with(context).asBitmap().override(Target.SIZE_ORIGINAL).load(resizedBitmap).into(imagePreview);
                            imagePreview.animate().alpha(1f).setDuration(250);
                        }
                    });
                    Toast.makeText(CreateSpot.this,"Unable to get location from image.",Toast.LENGTH_SHORT).show();
                }

                Glide.with(context).asBitmap().load(filePath).into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource,@Nullable Transition<? super Bitmap> transition) {
                        Bitmap resizedBitmap = getResizedBitmap(resource,resource.getHeight() / 3);
                        Glide.with(context).asBitmap().override(Target.SIZE_ORIGINAL).load(resizedBitmap).into(imagePreview);
                        imagePreview.animate().alpha(1f).setDuration(250);
                    }
                });
            } catch (IOException e) {
                Log.d("error",String.valueOf(e));
            } catch (NullPointerException ne) {
                Glide.with(context).asBitmap().load(filePath).into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource,resource.getHeight() / 3);
                        Glide.with(context).asBitmap().override(Target.SIZE_ORIGINAL).load(resizedBitmap).into(imagePreview);
                        imagePreview.animate().alpha(1f).setDuration(250);
                    }
                });
                Toast.makeText(CreateSpot.this,Toast.LENGTH_SHORT).show();
            } catch (ImageProcessingException e) {
                e.printstacktrace();
            }
        } else if (requestCode == CAMERA_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                Glide.with(context).asBitmap().load(filePath).into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource,resource.getHeight() / 3);
                        Glide.with(context).asBitmap().override(Target.SIZE_ORIGINAL).load(resizedBitmap).into(imagePreview);
                        imagePreview.animate().alpha(1f).setDuration(250);
                    }
                });
            } else {
                getContentResolver().delete(filePath,null,null);
            }
        }
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。