2017-10-05 114 views
0

下載的文件,我有我下載的文件與下載管理器這樣的應用程序,下載一些語音文件,然後播放它們(與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)); 

現在,我可以確認的是,文件下載和可播放 但我需要一個File打開它們,然後將該文件傳遞給MediaPlayer的

我是這樣做的:。

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 
} 

我存儲在SharedPreferences卸載ID與UID和使用下載管理器來即刪除文件downloadmanager.remove(id)這工作正常和正確的文件被刪除。

我在做什麼錯?

感謝

回答

1

而是從Uri通過getUriForDownloadedFile重新調整獲取文件的路徑,只要使用Uri作爲數據源爲MediaPlayer

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