0

code

import RNFetchBlob from 'rn-fetch-blob';

      RNFetchBlob.android
        .actionViewIntent(
          '/storage/emulated/0/Android/data/aaa.bbb.ccc/files/184.apk',
          'application/vnd.android.package-archive',
        )
        .then(() => {
          console.log('success');
        })
        .catch(err => {
          console.log('error');
        });

In the simulator, the above code works normally, and pops up the installation APK interface

On the phone (Android 8.1), the screen flashed white, then there was nothing, and the installation interface could not pop up, No error message

ebyte
  • 1,469
  • 2
  • 17
  • 32

2 Answers2

1

you should add permission in the AndroidMainFest.xml:

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

but because it is dynamic permission, I suggest you every time before you download and install APK to check it if it has permission.

In this situation, you can write a native module, for dynamic check permission you can see this answer after it goes to the permission set page

you can also use the react-native-permission library.

requestInstallUpdate = async () =>  {
  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.REQUEST_INSTALL_PACKAGES,
      {
        'title': 'Test App',
        'message': 'Test App needs to install updates.'
      }
    )
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log("You can now install updates")
      return true;
    } else {
      console.log("App install permission denied")
      return false;
    }
  } catch (err) {
    console.warn(err)
    return false;
  }
}

then in your download method

let canInstall = await requestInstallUpdate() 
if(canInstall){
  //download apk
}
Lenoarod
  • 3,441
  • 14
  • 25
0

Found out the reason for this, it's a know bug of the library and has a PR waiting to be merged (no timeframe from the repo owner). Here is the link to the PR: https://github.com/joltup/rn-fetch-blob/pull/317

So basically this needs to be added to line 122-123 of file android/src/main/java/com/RNFetchBlob/RNFetchBlob.java:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); If above is not working do to the below step: overwrite the 121 line in android/src/main/java/com/RNFetchBlob/RNFetchBlob.java:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 121 line intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // 122 line

  • Thank you, but I have tried the following two methods, both have no effect 1: ```jsx intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // + ``` --- 2: ```jsx intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // + ``` – ebyte Dec 06 '19 at 12:19