apache / apache/cordova-plugin-file-transfer

`download` method failing on cordova / android platform with `java.lang.NullPointerException` exception

Open
#374 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
606
Forks
874
PR merge metrics
No merged PRs in 30d

Description

Hi !
I'm currently unable to make the plugin work properly.
`FileTransfer.download` is failing with an error code 3 (`FileTransferError.CONNECTION_ERR`) and an exception `java.lang.NullPointerException`.

## Minimal example

```sh
cordova create . com.example.my_app "My App"
cordova platform add android
cordova plugin add cordova-plugin-file-transfer
```

Add this child to the `widget` element in `config.xml`.

```xml

```

Add a call to the `demo` function below from within `onDeviceReady`.

```js
function demo() {
window.requestFileSystem(window.PERSISTENT, 5 * 1024 * 1024,
fs => fs.root.getFile('cordova_bot.png', { create: true, exclusive: false },
fileEntry => ft_approach(fileEntry),
error => console.log(`file creation error: ${error}`)
),
(error) => console.log(`fs loading error: ${error}`)
);
}

function ft_approach(fileEntry) {
const fileTransfer = new FileTransfer();

fileTransfer.download(
'https://cordova.apache.org/static/img/cordova_bot.png',
fileEntry.toURL(),
() => window.alert('download complete'),
error => console.log(`download error: ${JSON.stringify(error, undefined, 2)}`),
false,
);
}
```

Run the app on the Android Studio `emulator` and attach Chrome DevTools .
The output should be something like:

```
download error: {
"code": 3,
"source": "https://cordova.apache.org/static/img/cordova_bot.png",
"target": "https://localhost/__cdvfile_persistent__/cordova_bot.png",
"http_status": 200,
"body": null,
"exception": "java.lang.NullPointerException"
}
```

## Workaround

Using web APIs.
Replacing `ft_approach` with the `xhr_approach` function below works, although not idea for large files.

```js
function xhr_approach(fileEntry) {
const xhr = new XMLHttpRequest();

xhr.open('GET', 'https://cordova.apache.org/static/img/cordova_bot.png', true);
xhr.responseType = 'blob';
xhr.onload = function () {
const blob = xhr.response;
if (xhr.status != 200) {
console.error(`request error ${xhr.status}: ${xhr.statusText}`);
return;
}
fileEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = () => window.alert('download complete');
fileWriter.onerror = error => console.log(`file writing error: ${error}`);
fileWriter.write(blob);
});
};
xhr.onerror = function() {
console.log('request error')
}
xhr.send();
}
```

Note: Update CSP accordingly

```html

```

## Versions

```
Windows 10 Home
Node 18.17.1
Cordova 12.0.0
cordova-android 12.0.1
cordova-plugin-file 8.0.0
cordova-plugin-file-transfer 2.0.0
Android SDK 33
Gradle 8.3
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the failure using the documented Cordova Android setup and the ft_approach function calling FileTransfer.download, then trace the Android-side handling of that entry point. Compare its behavior with the working xhr_approach and verify that the same download completes without error code 3 or a NullPointerException.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, java, javascript
Domain
mobile-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.