【发布时间】:2021-12-18 09:15:25
【问题描述】:
我正在尝试使用Intent.ACTION_GET_CONTENT 在我的模拟器中打开下载目录。
我可以成功获取下载目录的路径并列出其中的所有文件,但我似乎无法有意打开它。它只显示Recent 目录。
成功记录该下载目录中的所有文件,因此它的路径不正确
File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath());
File[] files = f.listFiles();
if (files != null) {
for (File inFile : files) {
Log.d(TAG, "onPermissionGranted: File name: " + inFile.getName());;
}
}
但是在使用意图时:
public void openDirectory() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath());
Log.d(TAG, "openDirectory: " + uri);
//logs this: /storage/emulated/0/Download
intent.setDataAndType(uri, "file/*");
startActivity(Intent.createChooser(intent, "Select keystore"));
}
它总是显示Recent
而不是Downloads
我必须手动转到Downloads 选项卡
我怎样才能使意图转到Downloads 而无需切换?
【问题讨论】:
-
你试过
DownloadManager吗? -
不,我没有。为什么要使用下载管理器?
-
Google 获取 INITIAL_URI。
-
openDirectory()相当混乱的函数名称。更好的是 selectFile() 或 selectFileWithActionGetContent()。
标签: android android-intent android-external-storage