I need to download file to user's Download directory in React Native app using rn-fetch-blob, but seems like it is not compatible with Android 10, because I am getting error:

First I ask for permissions:
try {
const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
await actualDownloadPDF();
} else {
ToastAndroid.show('Nepovolili jste ukládání souborů do zařízení.', ToastAndroid.LONG);
}
} catch (err) {
console.warn(err);
}
then I try to download file:
const res = await RNFetchBlob.config({
addAndroidDownloads: {
useDownloadManager: true,
notification: true,
mime: 'application/pdf',
title: filename,
path: dirs.DownloadDir,
},
}).fetch('GET', url, {
Authorization: 'Bearer ' + Api.Bearer,
});
It seems like Android 10 requires from developer to open Intent where user select write permission to folder ( https://developer.android.com/training/data-storage/shared/documents-files#grant-access-directory), but I am not sure how to do it, because it seems like RNFetchBlob does not have this feature, yet.
Any ideas how to make this work? It was working fine on Android 9.
const fileName = 'document.pdf';
const destPath = RNFetchBlob.fs.dirs.DownloadDir + '/' + fileName;
const config = {
fileCache : true,
path: destPath,
addAndroidDownloads: {
title: destPath,
description: `Download ${destPath}`,
useDownloadManager: false,
notification: false,
}
};
const res = await RNFetchBlob.config(config)...
I'm using this code and it works on Android 10.
Add android:requestLegacyExternalStorage="true" to in your AndroidManifest.xml
<application
...
android:requestLegacyExternalStorage="true"
>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With