android - 无法读取多个文件
问题描述
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, READ_REQUEST_CODE);
在活动结果中,我一直只选择一个文件。尽管我选择了多个文件。为什么 ?
List<Uri> files = Utils.getSelectedFilesFromResult(resultData);
// resultData.putExtra(EXTRA_ALLOW_MULTIPLE, true);
System.out.println(resultData.getBooleanExtra(EXTRA_ALLOW_MULTIPLE,false)+"read request code"+files.size());
for (Uri uris : files) {
uri = uris;//Uri.parse(uris.getPath().toString().replaceFirst("files", Environment.getExternalStorageDirectory().getPath()));
// file2 = Utils.getFileForUri(uri);
// Do something with the result...
}
解决方案
你可以试试这个onActivityResult()
:
if(resultCode == Activity.RESULT_OK) {
if(data.getClipData() != null) {
int count = data.getClipData().getItemCount();
for(int i = 0; i < count; i++)
Uri imageUri = data.getClipData().getItemAt(i).getUri();
//do something with the image (save it to some directory or whatever you need to do with it here)
}
} else if(data.getData() != null) {
String imagePath = data.getData().getPath();
//do something with the image (save it to some directory or whatever you need to do with it here)
}
}
推荐阅读
- assembly - 十进制到二进制转换 ASM 3 位
- javascript - 如何更改纸牌游戏图标?
- javascript - 如何使用 Selenium 从 JavaScript 窗口获取文本?
- postgresql - PostgreSQL `createuser` 没有指定用户名
- python - Python3 - 而 ids > Stop: TypeError: unorderable types: str()> int()
- ios - 保存视频后无法导航到另一个控制器
- swift - 使用 swift 从另一个函数访问闭包内的变量
- android-studio - log4j 运行时 NoClassDefFoundError
- python - 获取scrapy不同部分的数据列表
- android - 反应原生 AndroidManifest 不生效