列出Android手机的“下载"目录中的文件 [英] List the files in Download directory of the Android phone

查看:274
本文介绍了列出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.

这篇关于列出Android手机的“下载"目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆