question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RNFetchBlob.android.actionViewIntent is opening an empty view.

See original GitHub issue

I’m using rn-fetch-blob 0.11.2 with react-native 0.61.3 to download files. While it works perfectly on iOS, I have an issue on android. It happens on android 10 (8 & 9 seem fine).

My code:

const DIRS = RNFetchBlob.fs.dirs
const FileManager = Platform.select({
  ios: {
    getDownloadPath: filename => DIRS.CacheDir + '/' + filename,
    preview: path => RNFetchBlob.ios.openDocument(path)
  },
  android: {
    getDownloadPath: filename => DIRS.DownloadDir + '/' + filename,
    preview: RNFetchBlob.android.actionViewIntent
  }
})

const path = FileManager.getDownloadPath(filename)
try {
  const fetchConfig = {
    fileCache: true,
    path,
    appendExt: filename.split('.').slice(-1)[0],
    addAndroidDownloads: {
      title: filename,
      path,
      notification: true,
      mime: mimeType,
      useDownloadManager: true,
      mediaScannable: true
    }
  }
  this.currentTask = RNFetchBlob.config(fetchConfig)
    .fetch('GET', href, {
      Authorization: config.credentials.header
    })
    .progress((received, total) => {
      this.setState({ received, total })
    })
  const response = await this.currentTask
  const downloadPath = response.path()
  FileManager.preview(downloadPath, mimeType)
  this.currentTask = null
} catch (error) {
  this.currentTask = null
}

When I run this on android I’m getting this screen: Screenshot_1572863097

The file is correctly downloaded, the dl manager notification appears and if I tap the notification then the file is opened. But calling RNFetchBlob.android.actionViewIntent right at the end of the download won’t open the file.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:4
  • Comments:6

github_iconTop GitHub Comments

3reactions
gorbatenkovcommented, Oct 1, 2020

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

2reactions
R4DIC4Lcommented, Jan 17, 2021

I also had this problem, you need to add request legacy for android 10, as stated by @gorbatenkov, but also request the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions (I believe only read is needed for opening a file with action view intent, if you don’t also need to download the file from a server before-hand). Requesting permissions is done via manifest for older android versions (before SDK version 23) as below:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

For newer android versions, it must be explicitly done before using the action view intent (see https://reactnative.dev/docs/permissionsandroid) as below:

import { PermissionsAndroid } from 'react-native';
const readGranted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE);
//OR
const writeGranted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE);
// For both, you can use as below and get as response the status in an array for each permission requested
const result = await PermissionsAndroid.requestMultiple([
    PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
    PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
  ]);
const isGranted = result[PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE] === 'granted' 
        && result[PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE] === 'granted';

You should do both to support devices before and after SDK version 23.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RNFetchBlob.android.actionViewIntent does not working on ...
I fixed this issue by adding "queries" in AndroidManifest.xml <queries> <intent> <action android:name="android.intent.action.VIEW" /> <data ...
Read more >
RNFetchBlob android, first view and then download the image ...
[Solved]-RNFetchBlob android, first view and then download the image-React Native. Search. score:1. android.actionViewIntent(PATH_OF_IMG, 'image/png').
Read more >
react-native-blob-util - npm
A module provides upload, download, and files access API. Supports file stream read/write for process large files.. Latest version: 0.17.0, ...
Read more >
types/rn-fetch-blob/index.d.ts - UNPKG
The CDN for @types/rn-fetch-blob. ... 123, readAsArrayBuffer(b: PolyfillBlob): void; ... For example, open Gallery app to view an image, or install APK.
Read more >
react-native-file-writer - npm package - Snyk
How to show the written/exported file on file manager? RNFetchBlob.android.actionViewIntent(path, "application/json"); will show it to the user.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found