【发布时间】: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