14

In my app I have a custom auto download and install APK it works like this

  // auto register for the complete download
     activity.registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));



 // Download the file through DownloadManager
 String destination = Environment.getExternalStorageDirectory() + "/";
    String fileName = "myfile.apk";
    destination += fileName;
    final Uri uri = Uri.parse("file://" + destination);
    DownloadManager.Request request = new  DownloadManager.Request(Uri.parse(apkUrl));
    request.setDescription("description");
    request.setTitle("title");
    request.setDestinationUri(uri);
    final DownloadManager manager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
    final long downloadId = manager.enqueue(request);

onComplete = new BroadcastReceiver() {
      public void onReceive(Context ctxt, Intent intent) {

          Intent install = new Intent(Intent.ACTION_VIEW);
          // BEFORE working doing this
          //install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          //install.setDataAndType(uri,
          //    manager.getMimeTypeForDownloadedFile(downloadId));

          // Using file provider it doesnt work
          Uri apkUri = FileProvider.getUriForFile(AutoUpdate.this,
              "com.myapp", file);
                install.setDataAndType(apkUri,manager.getMimeTypeForDownloadedFile(downloadId));
          activity.startActivity(install);
          activity.unregisterReceiver(this);

      }
    };

Android manifest:

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.myapp"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>

Provider_path (Sorry for some reason so cuts the path tag)

external-path name="myfolder" path="."/>

When the file finished to download the onComplete is called but the activiy doesn't start:

No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://com.myapp/myfolder/myfile.apk typ=application/vnd.android.package-archive flg=0x4000000 }

When using the normal file:// it does work

Is there something I'm missing when using the file provider? Does the activity doesn't start because the file is not found ? Do I need extra permission ? (at the moment I have INTERNET, READ and WRITE on external storage)

Johny19
  • 5,364
  • 14
  • 61
  • 99
  • `Does the activity doesn't start because the file is not found `. The message is that the activity is not found. – greenapps Sep 05 '16 at 14:53
  • Note that "if" I substitute the mime type by "*/*" it does open the app selection but when i chose the "file manager" it just opens it and stays at the root. That's why I'm wondering by using the FileProvider the file is not actually found ? – Johny19 Sep 05 '16 at 14:56
  • 1
    where have you declared "file" variable? – Raj Dec 11 '16 at 04:16
  • Why are you using "com.myapp" as authorities in your manifest but later you use "com.myapp" by hard-coding it when calling getUriForFile()? If your package name is com.company_name.application_name I think the authority must be "company_name", you don't have to add the ".com", are you sure you have a well formatted name for your package? – Windgate May 17 '21 at 12:06

3 Answers3

25

The package installer only supports content schemes starting on Android 7.0. Prior to that — and despite documentation to the contrary — the package installer only supports file schemes.

You will need to set the Uri on your Intent differently based on whether you are running on Android 7.0+ or not, such as by branching on Build.VERSION.SDK_INT.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
8

Commonware is right, For those looking for the code:

BroadcastReceiver onComplete = new BroadcastReceiver() {
       public void onReceive(Context ctxt, Intent intent) {

       downloadButton.setEnabled(true);
       downloadButton.setText("Terminado");
       progressbar.setVisibility(View.GONE);

       if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            Intent install = new Intent(Intent.ACTION_VIEW);
            install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            Uri apkURI = FileProvider.getUriForFile(
                     self,
                     self.getApplicationContext()
                     .getPackageName() + ".provider", file);
            install.setDataAndType(apkURI,
                    manager.getMimeTypeForDownloadedFile(downloadId));
            install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(install);
       } else{
            String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
            String fileName = "surveyevent.apk";
            destination += fileName;
            Uri uri = Uri.parse("file://" + destination);

            Intent install = new Intent(Intent.ACTION_VIEW);
            install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            install.setDataAndType(uri,
                    manager.getMimeTypeForDownloadedFile(downloadId));
            startActivity(install);
       }
       unregisterReceiver(this);
       finish();
  }
};
Md. Enamul Haque
  • 926
  • 8
  • 14
Saurabh Verma
  • 169
  • 1
  • 7
  • @lacas final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); – Saurabh Verma May 21 '19 at 22:28
  • 2
    what is downloadId? – Sathish May 20 '20 at 14:50
  • Why you append ".provider" to the package name? An Android package extension is always ".apk" and if you rename the apk file like this the system would never know about the type of the download. Have you ever tried to make that code run and work? – Windgate May 17 '21 at 11:57
0

Your provider is not found because

 android:enabled="true"

is missing in your manifest.

But then it will not work either reading the other answer of CW.

greenapps
  • 11,154
  • 2
  • 16
  • 19