Simon k
Simon k

Reputation: 413

Cant find file downloaded by the DownloadManager

I have an app that downloads some audiofiles and then plays them (with the MediaPlayer.

I download the files with the DownloadManager like this:

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

Now, I can verify that the files are downloaded and are playable. But I need to open them in a File and then pass that file to the MediaPlayer.

I do that like this:

    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
}

I store the download Id's in SharedPreferences with a uid and use the downloadmanager to i.e remove files downloadmanager.remove(id) This works fine and the correct file is removed.

What am I doing wrong?

Thanks

Upvotes: 0

Views: 277

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

Instead of getting file path from Uri retuned by getUriForDownloadedFile, just use Uri as datasource for MediaPlayer :

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

Upvotes: 1

Related Questions