Tarikhelian Tarikhelian - 4 months ago 10
Android Question

How to give a name to downloading file

I am downloading videos from the server using Android

DownloadManager
. I want to give a name to each video myself. By default the name of videos downloadfile-6.bin.

String servicestring = Context.DOWNLOAD_SERVICE;
DownloadManager downloadmanager;
downloadmanager = (DownloadManager) getSystemService(servicestring);
Uri uri = Uri.parse(links.get(0));
DownloadManager.Request request = new DownloadManager.Request(uri);
Long reference = downloadmanager.enqueue(request);


Suggestion will be appreciated.

Answer

You can set it via DownloadManager.Request object using setDestinationInExternalFilesDir(...) or setDestinationInExternalPublicDir(...). See the last line:

request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
       .setAllowedOverRoaming(false)
       .setTitle("DownloadTitle")
       .setDescription("DownloadDesc")
       .setDestinationInExternalPublicDir("/mypath", "filename.jpg");

Sources:

http://stackoverflow.com/a/16773944/5250273 https://developer.android.com/reference/android/app/DownloadManager.Request.html