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