7

I think i have a rather simple question.

http://www.vogella.com/blog/2011/06/14/android-downloadmanager-example/

I have been following a tutorial in the above url.

How do I change the filepath for downloads?

Thanks in Advance

Mich
  • 105
  • 1
  • 2
  • 9

4 Answers4

21

You configure the DownloadManager.Request object with that sort of information. In the tutorial, that Request object is created and used in onClick().

For example:

DownloadManager.Request req=new DownloadManager.Request(uri);

req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI
                               | DownloadManager.Request.NETWORK_MOBILE)
   .setAllowedOverRoaming(false)
   .setTitle("Demo")
   .setDescription("Something useful. No, really.")
   .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                                      "test.mp4");

(above code is from this sample project)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    The last line is the important one :) At first I stumbled a bit on that. – Dominik Mohr Jun 06 '12 at 00:25
  • Hi CommonsWare I am still not certain on how to tell DownloadManager what folder to store the files in :(. Something like you would do in Async OutputStream output = new FileOutputStream("/sdcard/Myfolder/file_name.extension"); – Mich Jun 06 '12 at 00:44
  • @Mich: `setDestinationInExternalPublicDir()` and related methods let you specify the output directory as a `File`, not as an `OutputStream`. – CommonsWare Jun 06 '12 at 10:50
  • Thanks. Just starting to understand it – Mich Jun 25 '12 at 19:01
  • @CommonsWare if I want to change destination to my aplication databases folder. what should I do? I have tried with this code `.setDestinationUri(dst_uri);` but it gives error **Caused by: java.lang.IllegalArgumentException: Not a file URI:** – mehmet Apr 11 '14 at 08:39
  • @mehmet: `DownloadManager` cannot write to your app's internal storage (e.g., `getDatabasePath()`). Beyond that, whatever `dst_uri` is does not have the `file://` scheme, apparently. – CommonsWare Apr 11 '14 at 11:52
  • @CommonsWare I found this solution what u think. `Environment.getExternalStoragePublicDirectory(dst_uri.getPath()).mkdirs();` `request.setDestinationInExternalPublicDir(dst_uri.getPath(), "file name with . ext" );` – mehmet Apr 11 '14 at 11:58
  • @mehmet: `getExternalStoragePublicDirectory()` does not take a path as a parameter. – CommonsWare Apr 11 '14 at 12:01
5

The last line in CommonsWare's answer states the destination. He just uses the regular download-folder on the sdcard, but you could as well do this:

req.setDestinationInExternalPublicDir("/mnt/sdcard/Myfolder", "file_name.extension");
cjpa
  • 51
  • 2
  • 2
    What is the downside of using req.setDestinationUri(uri) or req.setDestinationInExternalPublicDir – Mich Jun 06 '12 at 01:37
  • 7
    You could also use `Environment.getExternalStorageDirectory()` instead of hardcoding to **"/mnt/sdcard"** – Kalarani Jul 02 '12 at 12:11
2

You make sure external storage access is permitted by user for your app. Include this code for the download

DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
     Uri uri = Uri.parse(url_string);
     DownloadManager.Request request = new DownloadManager.Request(uri);        
     request.setVisibleInDownloadsUi(true);        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    // include this line if permission has been granted by user to external directory
     request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, uri.getLastPathSegment());  
// or this line if not yet granted
request.setDestinationUri(Uri.parse("file://" + uri.getLastPathSegment());      
    Long reference = downloadManager.enqueue(request); 
ABODE
  • 958
  • 2
  • 15
  • 13
0

You can try to do following:

  1. Visit the URL to understand about Download Manager https://github.com/quoraboy/DownloadManagerInAndroid
  2. As far as I understand you can't pause/resume download manually.
  3. If you cancel the download then it totally depends on your server whether they support pause/resume feature or not. If yes then, after cancelling the download, if you start the download again it may actually resume it and don't start downloading from beginning.
Ivan Aracki
  • 4,861
  • 11
  • 59
  • 73