【问题标题】:I access the download folder but I cannot access the files in the download folder on android我访问了下载文件夹,但我无法访问 android 上下载文件夹中的文件
【发布时间】:2020-07-17 11:58:14
【问题描述】:

我访问了下载文件夹,但我无法访问 android 上下载文件夹中的文件。我想检查下载文件夹中文件的存在。例如,当我想从 Internet 下载一个名为 example.txt 的文件时,我想检查该文件是否在下载文件夹中。我寻找了很多解决方案,但我找不到。谢谢

File dir=  new File(Environment.
                getExternalStoragePublicDirectory(Environment
                        .DIRECTORY_DOWNLOADS).getAbsolutePath() + "/example.pdf");

        if(dir.exists()) {
           Toast.makeText(getApplicationContext(), "File Already Exists",
                    Toast.LENGTH_LONG).show();
        }

【问题讨论】:

  • 首先,使用new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "/belge.pdf");。它比您拥有的更短、更快、更可靠。其次,您需要通过这种方式访问​​external storage 的权限。第三,您在 Android 10 及更高版本上无法访问此位置。
  • 我添加了权限并尝试了“文件(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),”/belge.pdf“);”但它没有 vorking

标签: android file android-studio file-exists


【解决方案1】:

如果在Android10中使用,您可以尝试以下代码。

<application android:requestLegacyExternalStorage="true" ... >
    ...
  </application> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-16
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 2020-09-02
    • 2018-06-21
    • 2016-11-10
    • 2018-04-29
    相关资源
    最近更新 更多