Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting error while writing stream on android 14 #333

Open
ZubairAkber opened this issue Feb 7, 2024 · 2 comments
Open

Getting error while writing stream on android 14 #333

ZubairAkber opened this issue Feb 7, 2024 · 2 comments

Comments

@ZubairAkber
Copy link

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' }
@RonRadtke
Copy link
Owner

Can you please show the difference in code before and after your fix for Android 9?

@ZubairAkber
Copy link
Author

@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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants