0

I've managed to download the latest version programmatically on the user's phone, now I'm trying to send an intent to request it to install it.

I'm not using google play.

Here's the code:

Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + fileName)),
                "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ContextHolder.getInstance().getContext().startActivity(intent);

Here're the permissions in the manifest:

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

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

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera-autofocus" />

I'm getting this error:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: ro.clinicasante.viator, PID: 12019
android.os.FileUriExposedException: file:///storage/emulated/0/Download/app-debug-2.apk exposed beyond app through Intent.getData()
    at android.os.StrictMode.onFileUriExposed(StrictMode.java:2141)
    at android.net.Uri.checkFileUriExposed(Uri.java:2391)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:11152)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:11104)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1722)
    at android.app.ContextImpl.startActivity(ContextImpl.java:1023)
    at android.app.ContextImpl.startActivity(ContextImpl.java:994)
    at android.content.ContextWrapper.startActivity(ContextWrapper.java:403)
    at ro.clinicasante.viator.network.FTPDownloadLatestVersion.onPostExecute(FTPDownloadLatestVersion.java:81)
    at ro.clinicasante.viator.network.FTPDownloadLatestVersion.onPostExecute(FTPDownloadLatestVersion.java:21)
    at android.os.AsyncTask.finish(AsyncTask.java:771)
    at android.os.AsyncTask.access$900(AsyncTask.java:199)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:788)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
    I/Process: Sending signal. PID: 12019 SIG: 9
    Disconnected from the target VM, address: 'localhost:57651', transport: 'socket'

Any clue how I could solve it?

BaconSky
  • 21
  • 5
  • Note that `ACTION_VIEW` is deprecated for use for installs -- consider using `PackageInstaller`. Also note that the value for `` is `android.hardware.camera.autofocus`, not `android.hardware.camera-autofocus`. – CommonsWare May 09 '21 at 13:25
  • Thanks for the comment. Really helpfull. I've corrected te "camera.autofocus" issue, and I will inqure about the "PackageInstaller" library – BaconSky May 09 '21 at 17:10
  • `PackageInstaller` is a class in the Android SDK, not a library. See https://gitlab.com/commonsguy/cw-android-q/-/tree/vFINAL/AppInstaller for a Kotlin example that uses it. – CommonsWare May 09 '21 at 17:11
  • I'm only experienced in Java unfortunately – BaconSky May 09 '21 at 19:36

0 Answers0