列出Android手机的“下载"目录中的文件
我正在使用以下代码尝试在下载"目录中列出文件.
I am using the following code for trying to list the files in Download directory.
我构建了apk,将其安装在手机上,然后运行它.在手机的文件浏览器中查看时,内部存储器/下载文件夹和外部存储器/下载文件夹中都有文件,但该应用程序不显示文件列表.当我调试时,我发现listFiles()
函数返回null
.
I build the apk, install it on my phone and then run it. I have files in both Internal memory/Download folder and External memory/Download folder when I view in File Browser of my phone, but the app does not display the file list. When I debug I find that the listFiles()
function returns null
.
请让我知道我在哪里做错了.变量state
具有已装入的值,因此问题与未装入的内存无关.
Please let me know where I am doing wrong. The variable state
has value mounted so the issue has nothing to do with the memory not being mounted.
String state = Environment.getExternalStorageState();
private boolean isMediaAvailable() {
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
} else {
return false;
}
}
if (!isMediaAvailable()) {
Utility.finishWithError(this,"Media Not Available");
} else {
String path =Environment.getExternalStorageDirectory().toString()+ File.separator + Environment.DIRECTORY_DOWNLOADS;
File file = new File(path);
mRootPath = file.getAbsoluteFile().getPath();
mFileNames = new ArrayList<String>();
File filesInDirectory[] = file.listFiles();
if (filesInDirectory != null) {
for (int i = 0; i<filesInDirectory.length;i++) {
mFileNames.add(filesInDirectory[i].getName());
}
}
}
使用情况:
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
获取下载的目录,
并使用 File.list()获取包含目录中文件列表的数组.
and use File.list() to get an array with the list of files in the directory.