fuf-nf commented on issue #374: URL: https://github.com/apache/cordova-plugin-file-transfer/issues/374#issuecomment-2069208369
Hi, the following code is not working on Android.. ``` var example_external_file = "https://cordova.apache.org/static/img/cordova_bot.png" window.requestFileSystem(window.PERSISTENT, 128 * 1024 * 1024, function (fs) { fs.root.getFile("cordova_bot.png", {create: true}, function (fileEntry) { var target = fileEntry.toURL(); cordova.fileTransfer.download(example_external_file, target, function (success) { console.log('success', success); }, function (err) { console.log('error', err); }) }); }); ``` err is: ``` err = { body: null, code: 3, exception: "java.lang.NullPointerException", http_status: 200, source: "https://cordova.apache.org/static/img/cordova_bot.png", target: "https://localhost/__cdvfile_persistent__/cordova_bot.png" }; ``` the problem is the target path. is should be file:// and not https:// the problem maybe lies here https://github.com/apache/cordova-plugin-file/issues/619 With https:// fails this code in [Cordova-Plugin-File](https://github.com/apache/cordova-plugin-file-transfer/blob/f12b73eb0ba70536072eeab89843c96082fce512/src/android/FileTransfer.java#L728) > `file = resourceApi.mapUriToFile(targetUri);` > targetUri = "https://localhost/__cdvfile_persistent__/cordova_bot.png" > file = NULL and file=NULL leads to java.lang.NullPointerException Quickfix: fileEntry.toURL() returns: "https://localhost/__cdvfile_persistent__/cordova_bot.png" -> wrong fileEntry.toNativeURL() also returns: "https://localhost/__cdvfile_persistent__/cordova_bot.png" -> wrong fileEntry.nativeUrl is "file:///data/user/0/de.fuf.corodva-app/files/files/cordova_bot.png" -> fine Code with quickfix: ``` window.quickFix = false; function download_file(source, fileEntry, onSuccess, onError) { var target = fileEntry.toURL(); // if quirksMode is enabled, use the nativeURL instead of fileEntry.toURL if (window.quirksMode) { target = fileEntry.nativeURL; } cordova.fileTransfer.download(source, target, onSuccess, function (err) { // ony if the error appears and quirksMode is false if (err.code === 3 && window.quirksMode === false) { // enable quirksMode window.quirksMode = true; console.log('error', err, '-> enabling quirksMode') // and redownload the file.. download_file(source, fileEntry, onSuccess, onError); return; } onError(err); }); } window.requestFileSystem(window.PERSISTENT, 128 * 1024 * 1024, function (fs) { fs.root.getFile("cordova_bot.png", {create: true}, function (fileEntry) { download_file(example_external_file, fileEntry, function (succ) { console.log("success", succ); }, function (err) { console.log("error", err); }); }); }); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org For additional commands, e-mail: issues-h...@cordova.apache.org