Tuesday, July 4, 2017

Trigger android download manager Cordova

Leave a Comment

I'm trying to download files with a Cordova app, this is working fine for smaller files using the File transfer plugin, but for bigger files I would like to trigger the android download manager. Is this possible(probably with a plugin)?

I need to be able to change the headers of the request, so the 'cordova plugin background download' plugin didn't help me.

Edit: I already fixed my problem, without the use of the Android Download manager. Since most of the files my app will transfer are < 50mb triggering the DM wasn't absolutely necessary. This question has gotten a lot of attention because of a bounty that isn't mine. While I would still like a good answer I don't have much time to test the answers.

2 Answers

Answers 1

The best way to go is to write your own plugin. Writing Cordova plugins is easy, and it is enough just to follow official doc. We done the same because also it was necessary to customize download process. It looks like a native call using this method, because it activate native DM so user can see progress, cancel it, pause etc.

And once you register your own plugin this is code that you can use to start with:

    import android.app.DownloadManager;         import android.app.DownloadManager.Request;          public class DownloadPlugin extends CordovaPlugin{          private DownloadManager downloadManager;          public DownloadPlugin() {            downloadManager = (DownloadManager) webView.getContext()                                                                .getSystemService(webView.getContext().DOWNLOAD_SERVICE);         }          @Override         public boolean execute(String action, JSONArray args,                                CallbackContext callbackContext) throws JSONException {            String downloadUrl = args.getString(0);           startDownloadForUrl(String downloadUrl);          }          public void startDownloadForUrl(String downloadUrl){                 Request downloadRequest = new Request(downloadUrl);                 if (accessToken != null) { // we use oauth so this is example of changing download request                     downloadRequest.addRequestHeader("Authorization", "Bearer "                             + accessToken);                 }                 downloadRequest                 .setDescription("description")                 .setDestinationInExternalFilesDir("folderName", "fileName");                 downloadManager.enqueue(downloadRequest);           }         } 

Answers 2

This is one readymade downloader plugin which matches your requirement and comparitively active.

It also provides various options for downloading as mentioned in the usage link

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment