react-native-blob-util
react-native-blob-util copied to clipboard
Getting error while writing stream on android 14
I am trying to write stream using the following code below
let {dirs} = ReactNativeBlobUtil.fs;
const dirToSave = Platform.OS == 'ios' ? dirs.DocumentDir : dirs.DownloadDir;
let PATH_TO_FILE = dirs.DownloadDir + '/' + json.fileName.replace(/ /g, '') + '.pdf';
ReactNativeBlobUtil.fs.writeStream(
PATH_TO_FILE,
// encoding, should be one of `base64`, `utf8`, `ascii`
'base64',
// should data append to existing content ?
true
)
.then(stream => Promise.all([
stream.write(json.fileContent)
]))
// Use array destructuring to get the stream object from the first item of the array we get from Promise.all()
.then(([stream]) => {
stream.close()
ToastAndroid.show('File saved in downloads', ToastAndroid.LONG)
})
.catch(console.error)
It was working fine on android 14 but i test this on android 9 it is not writing the file so i found out that i need run time permission to write the file, so i just added and it starts working fine then but for android 14 it stops writing the file and start giving me the following error
{ [Error: File '/storage/emulated/0/Download/sTitleENFinal.pdf' does not exist and could not be created]
code: 'ENOENT',
line: 106879,
column: 30,
sourceURL: 'http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.package.name&modulesOnly=false&runModule=true' }
Can you please show the difference in code before and after your fix for Android 9?
@RonRadtke on android 9 i am calling for run time permissions before writing the stream, rest is same below is the code
requestReadWritePermission(){
console.log("Request Read Write Permission for Android");
return PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE
);
}
this.requestReadWritePermission()
.then(res => {
console.log(res)
if (res == PermissionsAndroid.RESULTS.GRANTED) {
console.log('read write permission granted' + JSON.stringify(res))
let {dirs} = ReactNativeBlobUtil.fs;
const dirToSave = dirs.DownloadDir;
let PATH_TO_FILE = dirs.DownloadDir + '/' + json.fileName.replace(/ /g, '') + '.pdf';
ReactNativeBlobUtil.fs.writeStream(
PATH_TO_FILE,
// encoding, should be one of `base64`, `utf8`, `ascii`
'base64',
// should data append to existing content ?
true
)
.then(stream => Promise.all([
stream.write(json.fileContent)
]))
// Use array destructuring to get the stream object from the first item of the array we get from Promise.all()
.then(([stream]) => {
stream.close()
ToastAndroid.show('File saved in downloads', ToastAndroid.LONG)
})
.catch(console.error)
} else {
console.log('read write permission not' + JSON.stringify(res))
}
})
.catch(console.error)