christo-pher18
christo-pher18

Reputation: 155

Read files function is crashing in RNFetchBlob in react native android

I am trying to share a pdf file in react native. But app is crashing on doing so. Please help. Following is the code

     async function func_1(
    pdfUrl: string,
   

  ) {
    let result = Platform.OS === 'android' && (await requestReadSmsPermission());
    let filePath: string | null = null;
    try {
      if (
        (result &&
          Platform.OS == 'android' &&
          result?.[PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE] ===
            PermissionsAndroid.RESULTS.GRANTED &&
          result?.[PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE] ===
            PermissionsAndroid.RESULTS.GRANTED) ||
        Platform.OS == 'ios'
      ) {
        const dirs = RNFetchBlob.fs.dirs;
      
        const dynamicFileName = `FileName_123.pdf`;
        const reportName = dynamicFileName;
        const downloadPath =
          Platform.OS === 'ios'
            ? (dirs.DocumentDir || dirs.MainBundleDir) + '/' + reportName
            : dirs.DownloadDir + '/' + reportName;

        RNFetchBlob.config({
          fileCache: true,
          path: downloadPath,
          addAndroidDownloads: {
            title: reportName,
            useDownloadManager: true,
            notification: true,
            path: downloadPath,
            mime: mimeType(downloadPath),
            description: 'File downloaded by download manager.',
          },
        })
          .fetch('GET', encodeURI(pdfUrl), {})
          .then(async (res) => {
           
            console.log({ res });
            filePath = res?.path();
            console.log({ filePath });

            const shareOptions = {
              title: dynamicFileName,
              url: `file://${res?.path()}`,
              failOnCancel: false,
              showAppsToView: true,
              mimeType: 'application/pdf',
            };
            Share.open(shareOptions);
          })
          .catch((error) => {
           console.log(error)
          });
      } else {

      }
    } catch (err) {
     console.log(err)
    }
  }

When i try above code, everything works, but while sharing the file, it says - Sharing failed, please try again later. (tried in whatsapp, teams). My file path comes up something like this - /storage/emulated/0/Download/FileName_123.pdf

When i tweek the code to the uri as base64, then app crashes, without giving any logs

RNFetchBlob.config(configOptions)
    .fetch('GET', pdfUrl)
    .then((resp) => {
      filePath = resp?.path();
      return resp?.readFile('base64'); //crashes here.
    })
    .then(async (base64Data) => {
      base64Data = `data:${type};base64,` + base64Data;
      await Share.open({ title: dynamicFileName, url: base64Data });
    })
    .catch((err) => {
     console.log(err)
    });

I am using "react-native-share": "^6.2.1" and "rn-fetch-blob": "^0.12.0" and react native version is "react-native": "0.67.4".

Please help kindoff stucked since a week. Checking on Android > 10

Upvotes: 0

Views: 1924

Answers (2)

madi
madi

Reputation: 1

Happened with me with file name either have '_' or ' ' in it. Remove these characters from name, should work. Working fine for me now.

Upvotes: 0

christo-pher18
christo-pher18

Reputation: 155

I got it working by changing the download directory path. It was not working due to permissions issue.

Upvotes: 0

Related Questions