繁体   English   中英

从onActivityResult中的图库图像中检索EXIF数据

[英]Retrieve EXIF data from gallery image in onActivityResult

目前,我正在从图库中获取图像,并使用它填充imageField。 我尝试从图像中提取位置信息(假设存在),但是我一直得到的只是空值。

我知道我正在测试的图像在其EXIF数据中包含纬度和经度条目。

我已经尝试了所有可以在网上找到的内容,但没有任何效果。 我认为问题在于将不正确的参数发送到ExifInterface。 这是我现在得到的:

...

else if (requestCode == REQUEST_IMAGE_FROM_GALLERY) {
        try {
            Uri selectedImage = data.getData();
            String currentImage = selectedImage.getPath()
            + File.separator + getFileName(selectedImage);
....

然后初始化ExifInterface:

....

try {
    ExifInterface ei = new ExifInterface(currentImage);
    imageLatitude = ei.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
    imageLongitude = ei.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
....

但是imageLatitude和imageLongitude返回null。 我什至可以替换掉发送到ExifInterface中的参数作为图像的完整路径( /storage/emulated/0/DCIM/Camera/IMG_20150119_170010.jpg ),但仍然得到空值。

到底应该将什么传递给ExifInterface?如果传递正确的参数,为什么会得到空值?

我最终使用以下方法使其工作:

private String getRealPathFromURI(Uri contentURI, Activity activity) {
    Cursor cursor = activity.getContentResolver()
                    .query(contentURI, null, null, null, null);
    if (cursor == null) { // Source is Dropbox or other similar local file
        // path
        return contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        return cursor.getString(idx);
    }
}

看到它在这里使用:

if (requestCode == REQUEST_IMAGE_FROM_GALLERY) {
        try {
            Uri selectedImage = data.getData();
            String currentImageFile = getRealPathFromURI(selectedImage, this);

            ExifInterface ei = new ExifInterface(currentImageFile);

这提供了适当的ExifInterface,使我可以从图像(存在的图像)中提取所需的数据。

感谢大家的意见和建议。

也许传递给ExifInterface的图像路径无效,您可以使用以下代码进行检查:

File file = new File(currentImage); 
if(file.exists()){ 
// print file exist
}else{
 // print file not exist.
}

如果有问题,可以通过传递有效的图像路径来解决。 如果不是问题,可以通过在以下代码之前添加断点来调试代码并查看ExifInterface ei对象中的属性:

imageLatitude = ei.getAttribute(ExifInterface.TAG_GPS_LATITUDE);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM