rn-fetch-blob icon indicating copy to clipboard operation
rn-fetch-blob copied to clipboard

Error: Permission denied

Open harleenarora opened this issue 5 years ago • 10 comments
trafficstars

HI, I am working on react-native new version, I have implement the html converter to pdf and got the base64 data. I have tried to download file in my Download folder. I am try download using this library but get the Permission denied error. I am add a android permissions, WRITE_EXTERNAL_STORAGE and <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>

But got the this error:

Error: Permission denied
    at Object.fn [as writeFile] (index.bundle?platfor…&minify=false:10730)
    at Object.writeFile (index.bundle?platfor…minify=false:334448)

Code:

createAndSavePDF = async() => {
        let options = {
            html: '<h1>This is a test PDF</h1>',
            fileName: 'test',
            directory: 'Download',
            base64: true
        };
        let file = await RNHTMLtoPDF.convert(options)

        // RNFetchBlob.fs.dirs.DownloadDir it's getting the download folder from internal storage
        let filePath = RNFetchBlob.fs.dirs.DownloadDir + '/testPDF.pdf';
        
        RNFetchBlob.fs.writeFile(filePath, file.base64, 'base64')
            .then(response => {
                console.log('Success Log: ', response);
            })
            .catch(errors => {
                console.log(" Error Log: ", errors);
            })
    }

I am working on debug mode on system.

harleenarora avatar Dec 18 '19 11:12 harleenarora

Got the same problem in Android 10 when trying to copy a file with cp to Downloads directory.

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

in AndroidManifesl.xml seems to help. Don't know if it is the issue with the library.

Pyroboomka avatar Feb 07 '20 15:02 Pyroboomka

android:requestLegacyExternalStorage="true" its not found on targetSDKversion:28

harleenarora avatar Feb 08 '20 03:02 harleenarora

On Android 10, you should to ask permissions through PermissionsAndroid

Try something like:

createAndSavePDF = async() => {
try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
      {
        title: 'Cool Photo App Camera Permission',
        message:
          'Cool Photo App needs access to your camera ' +
          'so you can take awesome pictures.',
        buttonNeutral: 'Ask Me Later',
        buttonNegative: 'Cancel',
        buttonPositive: 'OK',
      },
    );
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log('You can use the camera');
 let options = {
            html: '<h1>This is a test PDF</h1>',
            fileName: 'test',
            directory: 'Download',
            base64: true
        };
        let file = await RNHTMLtoPDF.convert(options)

        // RNFetchBlob.fs.dirs.DownloadDir it's getting the download folder from internal storage
        let filePath = RNFetchBlob.fs.dirs.DownloadDir + '/testPDF.pdf';
        
        RNFetchBlob.fs.writeFile(filePath, file.base64, 'base64')
            .then(response => {
                console.log('Success Log: ', response);
            })
            .catch(errors => {
                console.log(" Error Log: ", errors);
            })
    } else {
      console.log('Camera permission denied');
    }
  } catch (err) {
    console.warn(err);
  }
       
    }

Peretz30 avatar Feb 17 '20 10:02 Peretz30

same issue here on only oppo mobile phones only

waqaramjad avatar Oct 31 '20 17:10 waqaramjad

I'm also getting the same problem. @Peretz30 I asked for permission as well on the Android side through PermissionAndroid. After allowing permission, it is saying permission denied as an error. Anyone can help?

Haseeba393 avatar Nov 26 '20 18:11 Haseeba393

@Pyroboomka I tried your listed method and now it is working perfect

Haseeba393 avatar Nov 30 '20 08:11 Haseeba393

Got the same problem in Android 10 when trying to copy a file with cp to Downloads directory.

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

in AndroidManifesl.xml seems to help. Don't know if it is the issue with the library.

This one works for me

Antonin-Keuck avatar Jul 15 '22 08:07 Antonin-Keuck

i already have android:requestLegacyExternalStorage="true" in AndroidManifesl.xml. i always have permission denied no matter what i do.

try {
        const granted = await PermissionsAndroid.request(
          PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
          {
            title: 'Storage Permission Required',
            message: 'App needs access to your storage to download Photos',
          },
        );
        if (granted === PermissionsAndroid.RESULTS.GRANTED) {
          // Once user grant the permission start downloading
          console.log('Storage Permission Granted.');
          downloadImage();
        } else {
          // If permission denied then show alert
          alert('Storage Permission Not Granted');
        }
      } catch (err) {
        // To handle permission related exception
        console.warn(err);
      }

any fix in 2023 ? thanks

alainib avatar Sep 26 '23 16:09 alainib

It's still happening for me. Permission denied after trying everything mentioned here.

witherBattler avatar Dec 02 '23 19:12 witherBattler

same issue here, I have done everything, and not even request is popped up on simulator of android!

LearnAliSha avatar Apr 19 '24 16:04 LearnAliSha