无法访问Android中的下载文件

时间:2018-03-13 04:29:17

标签: java android android-studio android-download-manager

所以我使用以下代码从提供的URL下载.mp3歌曲:

private long DownloadData (Uri uri) {

    long downloadReference;

    // Create request for android download manager
    downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
    DownloadManager.Request request = new DownloadManager.Request(uri);

    request.setTitle("Data Download");
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | 
            DownloadManager.Request.NETWORK_MOBILE);
    request.setDescription("Android Data download using DownloadManager.");
    request.setDestinationInExternalFilesDir(DownloadSongActivity.this, 
            Environment.DIRECTORY_DOWNLOADS ,"doesn'twork.mp3");

    downloadReference = downloadManager.enqueue(request);

    return downloadReference;
}

然后,我尝试使用以下代码为用户提供对这些文件的访问权限

File path = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
    File[] downloadedFiles = path.listFiles();


    if(downloadedFiles == null){
        System.out.println("DownloadedFiles are null");
    } else if(downloadedFiles.length == 0){
        System.out.println("DownloadedFiles are empty");
    } else{
        System.out.println("DownloadedFiles are not empty");
    }

然而,似乎downloadedFiles总是为空。有什么建议。 以下是一些图片供参考

如果我去文件 - >下载

Image Reference link

1 个答案:

答案 0 :(得分:0)