react-native-blob-util icon indicating copy to clipboard operation
react-native-blob-util copied to clipboard

Android 13: "Permission denied" for Download file

Open ChetanPatelSS opened this issue 2 years ago • 6 comments

Downloading file, it's not working on Android 13 devices at all anymore

I tried to write a file to RNFetchBlob.fs.dirs.DownloadDir but it always returns Permission Denied. After digging a bit more into it turns out Android 13 introduce a new storage paradigm called.

Sample code try { // Request permission to write to external storage (Android-specific) const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,{ title: 'storage title', message: 'storage_permission', buttonPositive: 'ok', } );

        console.log("handleDownload==granted=>",granted); //  never_ask_again for android 13
        if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
            ToastAndroid.show(
                'Permission denied. Cannot download file.',
                ToastAndroid.SHORT
            );
            return;
        }

        // File download URL
        const fileUrl = pdfUrl
        const fileName = 'test.pdf';

        // Download the file
        const res = await ReactNativeBlobUtil.config({
            fileCache: true,
            addAndroidDownloads: {
                useDownloadManager: true,
                notification: true,
                path: `${ReactNativeBlobUtil.fs.dirs.DownloadDir}/${fileName}`,
                mime: 'application/pdf',
                title: fileName,
                description: 'Downloading file...',
                mediaScannable: true
            },
        }).fetch('GET', fileUrl);

        ToastAndroid.show('File downloaded successfully!', ToastAndroid.SHORT);
    } catch (error) {
        console.log('File download error:', error);
        ToastAndroid.show(
            'An error occurred while downloading the file.',
            ToastAndroid.SHORT
        );
    }

ChetanPatelSS avatar Jul 28 '23 09:07 ChetanPatelSS

@ChetanPatelSS

You could try this:

const fileName = 'SomeFileName.ext';
const filePath = `${ReactNativeBlobUtil.fs.dirs.DocumentDir}/${fileName}`;

ReactNativeBlobUtil.MediaCollection.copyToMediaStore(
      {
        name: fileName,
        parentFolder: '',
        mimeType: 'application/octet-stream',
      },
      'Download',
      filePath
    );

It works for Android 13. For Android < 13 you could use:

ReactNativeBlobUtil.fs.writeFile(filePath, base64Content, 'base64');

mikasius avatar Aug 02 '23 09:08 mikasius

Doesn't work

'error from', [Error: No such file ('/data/user/0/com.bletest/files/0e072505-d1d3-40b5-8b1a-e2123dc205a9.jpg')]```

aje avatar Aug 14 '23 09:08 aje

For Android 13 RNFetchUtil.fs.dirs.DocumentDir 👍

You could try this:

let file_path = Platform.Version < 31 ? RNFetchUtil.fs.dirs.DownloadDir : RNFetchUtil.fs.dirs.DocumentDir + '/informe.pdf';

tripexito avatar Aug 25 '23 22:08 tripexito

Try adding the below lines in your AndroidManifest.xml

And Inside in the application tag,

<application .......................... ............................ android:requestLegacyExternalStorage="true">

NishilE avatar Nov 23 '23 17:11 NishilE

@ChetanPatelSS Did you find a solution for this issue?

FranciscoMendes10866 avatar Jul 26 '24 13:07 FranciscoMendes10866

@FranciscoMendes10866 I assume you can reproduce it. Can you please test if removing or changing any of the options works? For my app in production without download manager etc I don't have any problems.

So I assume it is either media scannable or the path for the downloadmanager. With newer android versions media scannable likely has no effect regardless.

RonRadtke avatar Jul 27 '24 05:07 RonRadtke