使用选择器从外部存储加载文件 [英] Load file from external storage using chooser

查看:116
本文介绍了使用选择器从外部存储加载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试加载一个简单的.txt文件,如下所示:

So, I'm trying to load a simple .txt file like this:

    private void showFileChooser() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("text/plain");
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    try {
        startActivityForResult(
                Intent.createChooser(intent, "Select a File to Upload"),
                FILE_SELECT_CODE);
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(this, "Please install a File Manager.",
                Toast.LENGTH_SHORT).show();
    }
}

当然,要捕获这样的结果:

And of course, catching the result like this:

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case FILE_SELECT_CODE:
            if (resultCode == RESULT_OK) {

                // Get the Uri of the selected file
                Uri uri = data.getData();

现在,如果我使用已安装的文件浏览器(文件浏览器,请参见上图),它在genymotion和我的设备上都很好用,如果直接使用选择器是这样的:

It works great on genymotion and on my device if I use a file explorer that I have installed (File explorer, see image abovew), now, If use the chooser directly like this:

它说找不到指定的文件.(FileNotFoundException)

It says it cannot find the specified file. (FileNotFoundException)

现在,我意识到从这两个文件选择器中获得的URI是不同的

Now, I've realized that the URIs I get from these two file choosers are different

content://com.android.externalstorage.documents/document/primary%3ADownload%2Ffile.txt<-此操作有效(Android内置于资源管理器中)

content://com.android.externalstorage.documents/document/primary%3ADownload%2Ffile.txt <- THIS DOESNT WORK (android built in explorer)

content://media/external/file/44751<-此工作(自定义资源管理器)

content://media/external/file/44751 <- THIS WORKS (custom explorer)

有人知道我为什么要为SAME文件获得不同的URI吗?

Does anyone have any idea why I'm getting different URIs for the SAME file.

我试图使用内容解析器从URI中获取文件路径,如下所示:

I tried to use a content resolver to get the file path from the URI like this:

public class Utils {

public static String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = {MediaStore.Files.FileColumns.DATA};
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(proj[0]);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

}

仍然没有运气:(

推荐答案

我试图使用内容解析器从URI这样获取文件路径

I tried to use a content resolver to get the file path from the URI like this

没有文件路径. Uri 不一定指向您可以在本地文件系统上访问的文件,就像该网页的URL不一定指向您可以在本地文件系统上访问的文件一样. Uri 可能:

There is no file path. A Uri does not necessarily point to a file that you can access on your local filesystem, just as the URL to this Web page does not necessarily point to a file that you can access on your local filesystem. The Uri might:

  • 代表另一个应用程序内部存储器中保存的文件
  • 表示数据库中的BLOB列值
  • 代表云端"中保存的文件,需要时可下载

使用 ContentResolver 和诸如 openInputStream()之类的方法读取由 Uri 表示的内容,就像使用> HttpUrlConnection 读取此网页的URL表示的内容. openInputStream() Uri 作为参数,并返回 InputStream .您将以与其他任何 InputStream 相同的方式使用该 InputStream (例如,中的 FileInputStream InputStream > HttpUrlConnection ).确切的机制将在很大程度上取决于基础数据(例如,读入字符串,传递给 BitmapFactory 以解码 Bitmap ).

Use a ContentResolver and methods like openInputStream() to read in the content represented by the Uri, just like you would use HttpUrlConnection to read in the content represented by the URL for this Web page. openInputStream() takes the Uri as a parameter and returns an InputStream. You would use that InputStream the same way you would any other InputStream (e.g., FileInputStream, InputStream from HttpUrlConnection). The exact mechanics of that will depend a lot on the underlying data (e.g., read in a string, pass to BitmapFactory to decode a Bitmap).

这篇关于使用选择器从外部存储加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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