PolCPP / nativescript-downloadmanager

A small library to download files with android DownloadManager from nativescript
9 stars 10 forks source link

How to remove file after download? #2

Open shastik opened 7 years ago

shastik commented 7 years ago

Hi,

thank you for your plugin.

My question is: How to remove file after download.

I tried to return id of last downloaded file in:

//downloadmanager.android.js
if (this.downloads.has(id)) {
    var uri = c.getString(c.getColumnIndex(android.app.DownloadManager.COLUMN_LOCAL_URI));
    var cb = this.downloads.get(id);
    cb(success, uri, id);
}

and then in my callback:

dm.downloadFile("https://bohosluzby.cirkev.cz/uploads/church_service.db", function(result,uri, id) {
    console.log(id);
    dm.manager.remove(id)
}

but app crashes with error:

An uncaught Exception occurred on "main" thread.
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=org.nativescript.bohosluzbyapp (has extras) } in com.tns.gen.android.content.BroadcastReceiver_frnal_ts_helpers_l58_c38__BroadcastReceiver@41f5b9e8
    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:808)
    at android.os.Handler.handleCallback(Handler.java:808)
    at android.os.Handler.dispatchMessage(Handler.java:103)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:5292)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: com.tns.NativeScriptException: 
Calling js method onReceive failed

Error: Cannot convert number to [J at index 0
File: "file:///data/data/org.nativescript.bohosluzbyapp/files/app/services/churchservicedb.service.js, line: 141, column: 33

StackTrace: 
    Frame: function:'', file:'file:///data/data/org.nativescript.bohosluzbyapp/files/app/services/churchservicedb.service.js', line: 141, column: 34
    Frame: function:'DownloadManager.handleDownloadEvent', file:'file:///data/data/org.nativescript.bohosluzbyapp/files/app/tns_modules/nativescript-downloadmanager/downloadmanager.js', line: 46, column: 21
    Frame: function:'BroadcastReceiver.onReceive', file:'file:///data/data/org.nativescript.bohosluzbyapp/files/app/tns_modules/tns-core-modules/application/application.js', line: 217, column: 22

    at com.tns.Runtime.callJSMethodNative(Native Method)
    at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1197)
    at com.tns.Runtime.callJSMethodImpl(Runtime.java:1061)
    at com.tns.Runtime.callJSMethod(Runtime.java:1047)
    at com.tns.Runtime.callJSMethod(Runtime.java:1028)
    at com.tns.Runtime.callJSMethod(Runtime.java:1018)
    at com.tns.gen.android.content.BroadcastReceiver_frnal_ts_helpers_l58_c38__BroadcastReceiver.onReceive(android.content.BroadcastReceiver.java)
    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:798)
    ... 9 more

Do you have a solution for deleting downloaded files after download is completed?

Thanks

PolCPP commented 7 years ago

Are you running it on Android N/7 by any chance?

Edit: Taking a quick look (still sleepy and all) it reminds me some issue i'm having with Android N trying to run a downloaded APK.

shastik commented 7 years ago

Not yet. I tried Android 4.4.2