【问题标题】:Opening download directory using Intent使用 Intent 打开下载目录
【发布时间】: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


【解决方案1】:

这就是你的做法:

 Intent intent=new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
 startActivity(intent);

【讨论】:

  • 我怎样才能只显示一种文件类型.keystore 并能够选择它?
  • 我不确定,但我认为你不能这样做,因为我们只能将预定义的类型作为类型发送给意图。我搜索了密钥库扩展意图但没有找到任何。所以不支持.keystore 类型作为扩展名。
  • 您现在也无法选择文件。好吧,用户可以选择一个文件,但不会通知您的应用程序。如果您想为您的应用选择文件,请使用 INITIAL_URI,如前所述。
  • 您是否尝试过为 mime 类型添加 putExtra?
  • @blackapps 我已经使用 INITIAL_URI stackoverflow.com/a/67554693/11110509 试用了您的解决方案,但它需要 API 级别为 29。对于低于 29 的设备还有其他方法吗?
猜你喜欢
  • 2017-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多