lucasferreira / react-native-send-intent

React Native Android module to use Android's Intent actions for send text to shareable apps or make phone calls or opening third party apps
418 stars 159 forks source link

android.os.FileUriExposedException on installRemoteApp() #101

Closed fauno closed 4 years ago

fauno commented 4 years ago

Hi, I can't seem to use this code to install a downloaded app:

SendIntentAndroid
  .installRemoteApp('http://10.4.23.68:8080/app-release.apk', 'app.apk')
  .then((installWasStarted) => console.log(installWasStarted));

Throws this:

W System.err: android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.app/files/app.apk exposed beyond app through Intent.getData()
W System.err:    at android.os.StrictMode.onFileUriExposed(StrictMode.java:1796)
W System.err:    at android.net.Uri.checkFileUriExposed(Uri.java:2354)
W System.err:    at android.content.Intent.prepareToLeaveProcess(Intent.java:8981)
W System.err:    at android.content.Intent.prepareToLeaveProcess(Intent.java:8942)
W System.err:    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1519)
W System.err:    at android.app.ContextImpl.startActivity(ContextImpl.java:791)
W System.err:    at android.app.ContextImpl.startActivity(ContextImpl.java:768)
W System.err:    at android.content.ContextWrapper.startActivity(ContextWrapper.java:356)
W System.err:    at android.content.ContextWrapper.startActivity(ContextWrapper.java:356)
W System.err:    at com.burnweb.rnsendintent.RNSendIntentModule$3.onResponse(RNSendIntentModule.java:450)
W System.err:    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:206)
W System.err:    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
W System.err:    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
W System.err:    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
W System.err:    at java.lang.Thread.run(Thread.java:761)
I ReactNativeJS: false

Both from master and 1.0.30 using RN 0.59, the target SDK is 28. The only changes I've made to the code are from PR #99. Though it's no clear what should happen, does the apk installer open and offers the user to install the apk? I've checked #78, and added the permission recommended here https://stackoverflow.com/questions/45726654/install-apk-programmatically-in-android-8-api-26

fauno commented 4 years ago

Made it work with this patch, should I make a PR?

--- node_modules/react-native-send-intent/android/src/main/java/com/burnweb/rnsendintent/RNSendIntentModule.java.orig   2019-09-26 16:15:51.284106362 -0300
+++ node_modules/react-native-send-intent/android/src/main/java/com/burnweb/rnsendintent/RNSendIntentModule.java    2019-09-26 16:15:55.907579564 -0300
@@ -426,8 +443,13 @@
           try (final ResponseBody body = response.body()) {
             saveFile(body);

+            Uri uri = Uri.fromFile(file);
+            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
+              uri = FileProvider.getUriForFile(reactContext, reactContext.getPackageName() + ".fileprovider", file);
+            }
+
             final Intent intent = new Intent(Intent.ACTION_VIEW)
-                                  .setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
+                                  .setDataAndType(uri, "application/vnd.android.package-archive");
             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);

             reactContext.startActivity(intent);
lucasferreira commented 4 years ago

Hi @fauno

The PR https://github.com/lucasferreira/react-native-send-intent/pull/99 it's about AndroidX + RN 0.60+ support. Do you think that your problem it's related to this scenario?

By the way if you could make some PR with your improvements that will be great ;)

fauno commented 4 years ago

The PR #99 it's about AndroidX + RN 0.60+ support. Do you think that your problem it's related to this scenario?

No, I was just giving context :)

By the way if you could make some PR with your improvements that will be great ;)

Done! #102