Rn-fetch-blob: actionViewIntent fails on android 9+

Created on 17 Jul 2019  路  5Comments  路  Source: joltup/rn-fetch-blob

I am calling my api to get a pdf file:

const { dirs } = RNFetchBlob.fs;
const saveDirectory = Platform.select({ ios: dirs.DocumentDir, android: dirs.DownloadDir });

RNFetchBlob
  .config({ path: `${saveDirectory}/${docName || "temp"}.pdf`})
  .fetch(method, url, headers, body)
  .then(resolve);

It successfully resolves. Then, I try to open it:

RNFB.android.actionViewIntent(pdf, "application/pdf");

After a little bit of waiting I get a YellowBox warning, with a code "EUNSPECIFIED".

It's happening on Honor 8x with android 9.
Works on OnePlus One with android 6.0.1

Most helpful comment

Solved it by manipulating with the native java code.

RNFetchBlob.java:

before:

// Set flag to give temporary permission to external app to use FileProvider
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

after:

// Set flag to give temporary permission to external app to use FileProvider
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

This is actually worth a separate PR.

All 5 comments

Solved it by manipulating with the native java code.

RNFetchBlob.java:

before:

// Set flag to give temporary permission to external app to use FileProvider
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

after:

// Set flag to give temporary permission to external app to use FileProvider
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

This is actually worth a separate PR.

@pavermakov nope, 0.10.16 does not work for me.

Hi @bockc,

I had the same issue, I fixed it adding a title in addAndroidDownloads

{
          fileCache: true,
          appendExt: 'pdf',
          addAndroidDownloads: {
            useDownloadManager: true,
            notification: true,
            title: *TITLE*,
            mime: 'application/pdf',
            description: *DESCRIPTION*,
            path: *PATH*,
          },
          path: *PATH*,
}

Now the android.actionViewIntent(res.path(), 'application/pdf'); works fine.

I noticed it doesn't work with the new content:// uri received from android picker Recent category folder. I am not sure if there is a different way to handle the recent files as I have not found any solution yet.

In https://developer.android.com/guide/components/intents-common#OpenFile it only says to use ACTION_OPEN_DOCUMENT action and specify the MIME type on Android 4.4 or higher instead of ACTION_GET_CONTENT. I assume this is the way it is implemented in RNFetchBlob actionViewContent.

Maybe for android 9+ an approach using ACTION_OPEN_DOCUMENT is needed instead of ACTION_VIEW as here: https://stackoverflow.com/questions/61356281/android-files-unable-to-get-file-path-from-content-uri-when-file-is-selected-f.

Was this page helpful?
0 / 5 - 0 ratings