android - 使用选择器从外部存储加载文件

标签 android file

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

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

当然,捕捉这样的结果:
 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 和我的设备上效果很好,现在,如果像这样直接使用选择器:

enter image description here

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

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

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

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



有谁知道为什么我会为同一个文件获得不同的 URI。

编辑:
我尝试使用内容解析器从 URI 中获取文件路径,如下所示:
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();
        }
    }
}

}

仍然没有运气:(

最佳答案

@CommonsWare 的 asnwer 必须是解决此问题的正确方法,但我不确定如何实现他的解决方案。
我最终做了@Paul Burke 在此链接上推荐的操作:

Android Gallery on KitKat returns different Uri for Intent.ACTION_GET_CONTENT

编辑

对于我的特殊情况,这就是我的代码的结束方式。我将其留在这里以供将来引用。 (我正在使用@CommonsWare 的解释)见上面他的回答。

               try {
                    InputStream inputStream = getContentResolver().openInputStream(uri);
                    BufferedReader br = null;
                    if (inputStream != null) {
                        br = new BufferedReader(new InputStreamReader(inputStream));

                        String line;
                        while ((line = br.readLine()) != null) {
                            text.append(line);
                            text.append("\n");
                        }
                    } else {
                        subscriber.onError(new InputException("There's something seriously wrong with this file"));
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                    subscriber.onError(e);
                }

关于android - 使用选择器从外部存储加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32981292/

相关文章:

android - Unity 3D 插件的 Google Drive 授权错误

Flutter 中的 Android Alarm Manager Plus 从不触发 oneShot

file - 录制 Jmeter 脚本时出现访问被拒绝问题

JAVA 为什么只有图像才能与 class.getResource 配合使用

c# - 关闭文件时删除

java - 获取文件中字符数的问题

android - 具有以下 fragment 边缘 View 的应用栏

Android Lollipop 工具栏在打开/关闭抽屉和后退按钮之间切换

android - adb命令设置语音助手

c++ - 从文件中读取最后两次出现