列出Android手机下载目录中的文件

列出Android手机下载目录中的文件,android,list,file,download,Android,List,File,Download,我使用以下代码尝试列出下载目录中的文件 我建立了apk,安装在我的手机上,然后运行它。当我在手机的文件浏览器中查看文件时,我的内存/下载文件夹和外存/下载文件夹中都有文件,但应用程序不显示文件列表。调试时,我发现listFiles()函数返回null 请让我知道我哪里做错了。变量state已装入值,因此问题与未装入的内存无关 String state = Environment.getExternalStorageState(); private boolean isMediaAvailabl

我使用以下代码尝试列出下载目录中的文件

我建立了apk,安装在我的手机上,然后运行它。当我在手机的文件浏览器中查看文件时,我的内存/下载文件夹和外存/下载文件夹中都有文件,但应用程序不显示文件列表。调试时,我发现
listFiles()
函数返回
null

请让我知道我哪里做错了。变量
state
已装入值,因此问题与未装入的内存无关

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());
            }
        }

    }
String state=Environment.getExternalStorageState();
私有布尔值isMediaAvailable(){
if(环境、介质、安装等于(状态)){
返回true;
}否则{
返回false;
}
}
如果(!isMediaAvailable()){
Utility.finishWithError(此“媒体不可用”);
}否则{
字符串路径=Environment.getExternalStorageDirectory().toString()+File.separator+Environment.DIRECTORY\u下载;
文件=新文件(路径);
mRootPath=file.getAbsoluteFile().getPath();
mFileNames=新的ArrayList();
File filesInDirectory[]=File.listFiles();
if(filesInDirectory!=null){
对于(int i=0;i使用:

要获取下载的目录


和用于获取包含目录中文件列表的数组。

将存储权限添加到项目清单文件:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
将此方法添加到代码中:

public static boolean hasPermissions(Context context, String... permissions) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}
并且可以选择覆盖此选项:

    @Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1: {

            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.
                Toast.makeText(getApplicationContext(), "permission was granted", Toast.LENGTH_SHORT).show();

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
            }
            return;
        }
    }
}
现在,您可以使用以下代码访问应用程序下载目录:

context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);

祝你好运:)

file.getAbsolutePath()的值是多少?请给出内部和外部下载文件夹的完整路径。没有内部下载文件夹,所以我想知道你的意思。file.getAbsolutePath返回/storage/sdcard/download。所谓内部下载文件夹,我指的是当我从手机中取出SD卡时显示在内部存储下的下载文件夹。那么那么路径?显示在内部存储下?显示在哪里?/Storage/sdcard/Download是您使用文件资源管理器转到的目录,其中包含文件?您在清单文件?files.list()中请求了读取外部存储权限为我返回null。你知道6.0需要什么权限吗?是的,你必须为应用程序授予存储权限。否则列表将为空。我已经添加了以下3个权限:这是不够的。用户在第一次打开应用程序时必须授予权限(电话设置/应用程序/你的应用程序/权限)。如果你在清单中授予权限,并不意味着用户也会接受授予该应用程序权限(这是一个安全问题)…这在API 29中已被弃用。对此,您不需要WRITE_EXTERNAL_STORAGE权限,并且您的答案不会发出实际的权限请求,例如:String[]requiredPermissions={Manifest.permission.READ_EXTERNAL_STORAGE};ActivityCompat.requestPermissions(This,requiredPermissions,1);
    @Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1: {

            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.
                Toast.makeText(getApplicationContext(), "permission was granted", Toast.LENGTH_SHORT).show();

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
            }
            return;
        }
    }
}
context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);