-
Notifications
You must be signed in to change notification settings - Fork 889
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
NullPointerException When Downloading File #378
Comments
Analysis at a glanceBased on the stacktrace, it's failing at https://github.com/apache/cordova-plugin-file-transfer/blob/master/src/android/FileTransfer.java#L796 which is when it's trying to create the output stream for the downloaded file to local disk. the It would be helpful if you can confirm and provide the value used for the local file path in the WorkaroundAssuming that what I think above is correct, it's caused by your app passing in a The workaround would be using I'm still going to tag this as a bug, the |
I have changed the filePath to |
This path doesn't make sense, the directory must exists and That's why I suggested to use the The constants provides the base path for your platform. On android it will look something like Additionally if you use a subdirectories, then you'll also have to make sure the directories exists first, and if not create them before attempting to download the file. |
Just for context i am using a Library called ImgCache in order to download and save images on an android device: It uses the file transfer plugin to save the file and this is a picture of the implementation of the function that should save the picture I have attempted to replace the This is the output of the console.log that is seen in the picture above: |
I see. I glanced over that library, it's obvious it was built some time ago. It uses There's some history with that API and with the In older versions of Cordova, where the webview was loaded over the Nowadays, there's a lot more restrictions in the name of privacy/security. Cordova uses a newer API called WebViewAssetLoader. This is what maps But that means I admit this is kind of messy, but for modern platforms the library should probably use Simply replacing the If the original author is not around for that image cache library, it may need to be forked and updated so that it supports the current versions of the cordova plugins, if their license permits so. |
Bug Report
Problem
What is expected to happen?
The file is downloaded and stored onto the device.
What does actually happen?
An error showing that the file name is null and throws null pointer exception
Information
{"code":3,"source":"https:\/\/sample.io\/images\/logo.png","target":"https:\/\/localhost\/__cdvfile_persistent__\/imgcache\/04a568427e026c9d4dc0275d9ba360c7a24326be.png","http_status":200,"exception":"java.lang.NullPointerException"} java.lang.NullPointerException at java.io.FileOutputStream.<init>(FileOutputStream.java:227) at java.io.FileOutputStream.<init>(FileOutputStream.java:186) at org.apache.cordova.filetransfer.FileTransfer$2.run(FileTransfer.java:796) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:923)
Command or Code
`var fileTransfer = new Private.FileTransferWrapper(ImgCache.attributes.filesystem);
fileTransfer.download(
img_src,
filePath,
function (entry) {
entry.getMetadata(function (metadata) {
if (metadata && ('size' in metadata)) {
ImgCache.overridables.log('Cached file size: ' + metadata.size, LOG_LEVEL_INFO);
Private.setCurrentSize(ImgCache.getCurrentSize() + parseInt(metadata.size, 10));
} else {
ImgCache.overridables.log('No metadata size property available', LOG_LEVEL_INFO);
}
});
ImgCache.overridables.log('Download complete: ' + Helpers.EntryGetPath(entry), LOG_LEVEL_INFO);
Environment, Platform, Device
Android@13.0
Android Tablet
Version information
Android Studio: Latest version as of this posting
Cordova Plugins:
cordova-plugin-device 3.0.0 "Device"
cordova-plugin-file-transfer 2.0.0 "File Transfer"
cordova-plugin-file 8.1.0 "File"
Checklist
The text was updated successfully, but these errors were encountered: