gpt4 book ai didi

android - 如何获取下载文件夹中的文件名列表?

转载 作者:行者123 更新时间:2023-11-29 19:03:19 24 4
gpt4 key购买 nike

我试过这个:

File f =Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

System.out.println(f.list());
System.out.println(f.listFiles().toString());

但它显示为空。我已在 list 中授予读写权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

最佳答案

READ_EXTERNAL_STORAGE危险权限你需要请求它的运行时权限

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.

您需要ask runtime permission

也试试这个

试试这个

private void getfiles() {
String[] filenames = new String[0];
ArrayList<String> fileArrayList = new ArrayList<>();
File path = new File(Environment.getExternalStorageDirectory() + "/folder_name");
if (path.exists()) {
filenames = path.list();
}
for (int i = 0; i < filenames.length; i++) {
fileArrayList.add(path.getPath() + "/" + filenames[i]);
Log.e("fileArrayList", fileArrayList.get(i));

}
}

关于android - 如何获取下载文件夹中的文件名列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48077152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com