rn-fetch-blob: Can't open pdf after download

Configurations of my pdf

CONTENT-TYPE | application/octet-stream

My code

const downloadPdf = (uri: string, nome: string): void => {
    setLoading(true)
    const {config, fs} = RNFetchBlob
    const PictureDir = fs.dirs.DownloadDir
    const options = {
      fileCache: true,
      addAndroidDownloads: {
        useDownloadManager: true,
        notification: true,
        title: nome,
        path: `${PictureDir}/${nome}`,
      },
      appendExt: 'pdf',
    }
    config(options)
      .fetch('GET', uri, {
        // Accept: 'application/json',
        // 'Content-Type': 'multipart/form-data,octet-stream',
        'Content-Type': 'multipart/form-data',
      })
      .then((_) => {
        Snackbar.show({
          text: 'Download realizado com sucesso!',
          backgroundColor: colors.silverTree,
        })
      })
      .catch((_) => {
        Snackbar.show({
          text: 'Error ao realizar download!',
          backgroundColor: colors.valencia,
        })
      })
      .finally(() => setLoading(false))
  }

About this issue

Most upvoted comments

Was able to fix it by adding android:requestLegacyExternalStorage=“true” to my manifest file. Based on this https://medium.com/@sriramaripirala/android-10-open-failed-eacces-permission-denied-da8b630a89df

Hi @adaerodriguez,

I’m doing exactly this in my app. It works well on iOS ! However, on Android, it goes to a black screen on

RNFetchBlob.android.actionViewIntent(res.path(), mimeType || 'application/pdf');

And come back to the app. If i touch my notification on my phone, it redirects me to the file, which has been successfully downloaded on my phone.

Would you know what causes this issue ?

RN: 0.63.2 RN-Fetch-Blob: 0.12.0

Same here, it opens the pdf app for a split second but comes back directly to my app. Pressing the notification or accessing through the file app works fine tho…

Só falta seu código pra abrir aquele file, né?

Same issue here, any update?

Here’s my code :

return RNFetchBlob.config({
    path,
    addAndroidDownloads: {
      useDownloadManager: true,
      path,
      notification: true,
      title: notificationTitle,
      mime: 'application/pdf',
      mediaScannable: true,
    },
  })
    .fetch('GET', apiUrl)
    .then((res) => {
      if (Platform.OS === 'android') {
        RNFetchBlob.android.actionViewIntent(res.path(), 'application/pdf');
      } else {
        RNFetchBlob.ios.openDocument(res.path());
      }

      return res;
    });

Before this I ask READ and WRITE permission on Android :

PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE
PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE

And I have the following in AndroidManifest :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
</intent-filter>

Thanks,

A mi me srivio este codigo tanto en Ios y Android

const { config, fs } = RNFetchBlob;
config({
    fileCache: true,
    addAndroidDownloads: {
        useDownloadManager: true,
        notification: true,
        path: fs.dirs.PictureDir + '/IU-APP/' + file.name.split('/').pop(),
    },
    path: fs.dirs.DocumentDir + '/' + file.name.split('/').pop()
}).fetch('GET', encodeURI(file.url)).then(async(res) => {
    if (Platform.OS === "ios") RNFetchBlob.ios.previewDocument(res.path());
}).catch(error => {
    showAlert('Error al descargar el documento!', 'danger')
})

The file is well downloaded. The thing i can’t do is open it with RNFetchBlob.android.actionViewIntent