GetRealPathFromUri总是得到空结果 [英] GetRealPathFromUri always get null result

查看:1221
本文介绍了GetRealPathFromUri总是得到空结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从乌里(选定从库图)真正的路径,但这个函数总是返回空值:

  //图像URI转换为图像文件的直接文件系统路径
公共字符串getRealPathFromURI(URI contentUri){

    光标光标= NULL;
    尝试 {

        的String []凸出= {MediaStore.Images.Media.DATA};
        光标= getActivity()getContentResolver()查询(contentUri,凸出,NULL,NULL,NULL);
        cursor.moveToFirst();
        INT与Column_Index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        返回cursor.getString(Column_Index中);
    } 最后 {

        如果(光标!= NULL){

            cursor.close();
        }
    }
}
 

解决方案

这是因为你选择的形象不是物理设备上可用的。

从库或其他选择可以来自不同的来源不提供一个物理文件,像云存储为例图像。

在这里查看该code。关于如何打开乌里的InputStream 可用于创建该文件的一个副本(或只是直接读取)。

<一个href="http://stackoverflow.com/questions/5657411/android-getting-a-file-uri-from-a-content-uri">Android:获取文件的Uri从内容乌里?

编辑:

我在做它的一些额外的研究,显然它也各不相同的的如何的请求此URI。

如果你要求它这样的(这是当前preferred方法用于Android导游):

  I =新的意图(Intent.ACTION_GET_CONTENT);
 i.addCategory(Intent.CATEGORY_OPENABLE);
 i.setType(图像/ *);
 startActivityForResult(ⅰ,REQUEST_STORAG​​E);
 

和它打开奇巧风格的统一的选择,如果选择从那里的图像,它总是返回null。如果你打开​​左边的抽屉菜单中,选择库(或文件浏览器),并选择从那里图像,然后,如果它是一个本地文件的图像,你就会有正确的路径。

在另一方面,如果你要求它这样的(这是旧法):

  I =新的意图(Intent.ACTION_PICK);
 i.setType(图像/ *);
 startActivityForResult(ⅰ,REQUEST_STORAG​​E);
 

这将直接打开库,并再次,如果它是一个本地文件的图像,你就会有正确的路径。

所以是的,我还是不相信有一种方法,以当地唯一的强制,但至少你有更多的信息,使您的编码一个明智的决定。

I'm trying to get real path from Uri(Of selected image from gallery) but This function returns always null value :

//Convert the image URI to the direct file system path of the image file
public String getRealPathFromURI(Uri contentUri) {

    Cursor cursor = null;
    try {

        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = getActivity().getContentResolver().query(contentUri,  proj, null, null, null);
        cursor.moveToFirst();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        return cursor.getString(column_index);
    } finally {

        if (cursor != null) {

            cursor.close();
        }
    }
}

解决方案

That's because the image you're selecting is not physically available on the device.

Images from gallery or other choose can come from different sources that does not provide a physical file, like a cloud storage for example.

Check this code here on how to open the Uri as a InputStream that can be used to create a copy of the file (or just read directly).

Android: Getting a file Uri from a content Uri?

edit:

I'm doing some extra research on it, apparently it also varies on how you request this Uri.

if you request it like this (which is the current preferred method as per Android guides):

 i = new Intent(Intent.ACTION_GET_CONTENT);
 i.addCategory(Intent.CATEGORY_OPENABLE);
 i.setType("image/*");
 startActivityForResult(i, REQUEST_STORAGE);

and it opens the KitKat style unified selector, if you choose an image from there, it will always return null. If you open the left-drawer menu, select the gallery (or a file browser), and pick an image from there, then, if it is a local file image, you will have the correct path.

on the other hand, if you request it like this (which is the old method):

 i = new Intent(Intent.ACTION_PICK);
 i.setType("image/*");
 startActivityForResult(i, REQUEST_STORAGE);

it will directly open the Gallery, and again, if it is a local file image, you will have the correct path.

So yeah, I still don't believe there's a way to force local only, but at least you have more information to make an informed decision on your coding.

这篇关于GetRealPathFromUri总是得到空结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆