Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 找不到由DownloadManager下载的文件_Java_Android - Fatal编程技术网

Java 找不到由DownloadManager下载的文件

Java 找不到由DownloadManager下载的文件,java,android,Java,Android,我有一个应用程序可以下载一些音频文件,然后播放它们(使用MediaPlayer) 我使用DownloadManager下载文件,如下所示: DownloadManager downloadmanager = downloadManager = (DownloadManager) getApplication().getSystemService(Context.DOWNLOAD_SERVICE); long id = downloadManager.enqueue(new Down

我有一个应用程序可以下载一些音频文件,然后播放它们(使用MediaPlayer)

我使用DownloadManager下载文件,如下所示:

DownloadManager downloadmanager = downloadManager = (DownloadManager) getApplication().getSystemService(Context.DOWNLOAD_SERVICE);

     long id =  downloadManager.enqueue(new DownloadManager.Request(uri)
            .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
                                    DownloadManager.Request.NETWORK_MOBILE)
            .setAllowedOverRoaming(false)
            .setTitle(e.getValue().getName())
            .setDescription(getString(R.string.DOWNLOAD_MANAGER_TITLE))
            .setDestinationInExternalFilesDir(getApplication(),Environment.DIRECTORY_DOWNLOADS,fileName));
    Uri fileUri = dlMgr.getUriForDownloadedFile(id);

        File mAudioFile =new File(fileUri.getPath());

        if(mAudioFile.exists(){
     mediaPlayer.setDataSource(mAudioFile.getAbsolutePath());
    }else
{
//the file does not exist <-- this is where i end
}
现在,我可以验证文件是否已下载并可播放。 但是我需要在
文件中打开它们,然后将该文件传递给MediaPlayer

我是这样做的:

DownloadManager downloadmanager = downloadManager = (DownloadManager) getApplication().getSystemService(Context.DOWNLOAD_SERVICE);

     long id =  downloadManager.enqueue(new DownloadManager.Request(uri)
            .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
                                    DownloadManager.Request.NETWORK_MOBILE)
            .setAllowedOverRoaming(false)
            .setTitle(e.getValue().getName())
            .setDescription(getString(R.string.DOWNLOAD_MANAGER_TITLE))
            .setDestinationInExternalFilesDir(getApplication(),Environment.DIRECTORY_DOWNLOADS,fileName));
    Uri fileUri = dlMgr.getUriForDownloadedFile(id);

        File mAudioFile =new File(fileUri.getPath());

        if(mAudioFile.exists(){
     mediaPlayer.setDataSource(mAudioFile.getAbsolutePath());
    }else
{
//the file does not exist <-- this is where i end
}
urifileuri=dlMgr.geturifordownloaddedfile(id);
File mAudioFile=新文件(fileUri.getPath());
if(mAudioFile.exists()){
setDataSource(mAudioFile.getAbsolutePath());
}否则
{

//文件不存在而不是从
geturifordownloaddedfile
返回的
Uri
获取文件路径,只需使用
Uri
作为
MediaPlayer
的数据源:

if(fileUri!=null){
  mediaPlayer.setDataSource(fileUri);
}else{
   // fileUri is null
}