【问题标题】:Android pick file from 'Recent'Android 从“最近”中选择文件
【发布时间】:2016-01-18 00:34:03
【问题描述】:

下面是我为从我的应用程序中挑选文件编写的代码

Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
    intent.setType("*/*");
    intent.putExtra("return-data", true);

当我通过安装的其他文件管理器应用程序选择文件时,此代码工作正常。但是,从Recent 类别中选择文件时,将所选文件Uri 转换为绝对路径时失败。

Uri selectedImage = intent.getData();
String[] filePathColumn = {MediaStore.Files.FileColumns.DATA};
Cursor cursor = context.getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();

在上面的代码中,每当我从Recent 类别中选择文件时,我都会将picturePath 设为空。请帮我解决这个问题。

【问题讨论】:

  • 为什么要文件路径?您可以直接从给您的 Uri 打开图像。

标签: android android-intent android-cursor


【解决方案1】:

但在从最近类别中选择文件时,将所选文件 Uri 转换为绝对路径时失败

A Uri is not a file。没有可靠的“将所选文件 Uri 转换为绝对路径”的方法。请使用ContentResolveropenInputStream() 来使用Uri 支持的内容,就像使用HTTP 客户端API(例如HttpUrlConnection)来使用URL 支持的内容一样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多