react-native-blob-util
react-native-blob-util copied to clipboard
Android 13: "Permission denied" for Download file
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
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');
Doesn't work
'error from', [Error: No such file ('/data/user/0/com.bletest/files/0e072505-d1d3-40b5-8b1a-e2123dc205a9.jpg')]```
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';
Try adding the below lines in your AndroidManifest.xml
And Inside in the application tag,
<application .......................... ............................ android:requestLegacyExternalStorage="true">
@ChetanPatelSS Did you find a solution for this issue?
@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.