Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native download file using RNFetchBlob's Download Manager on Android 10

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:

enter image description here

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.

like image 942
Baterka Avatar asked Oct 24 '25 00:10

Baterka


2 Answers

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.

like image 116
Luis Abarca Avatar answered Oct 25 '25 15:10

Luis Abarca


Add android:requestLegacyExternalStorage="true" to in your AndroidManifest.xml

<application
  ...
  android:requestLegacyExternalStorage="true"
>
like image 29
Samson Akinfenwa Avatar answered Oct 25 '25 13:10

Samson Akinfenwa