No Activity found to handle Intent when using FileProvider

11,918

Solution 1

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.

Solution 2

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();
  }
};
Share:
11,918
Johny19
Author by

Johny19

Updated on June 11, 2022

Comments

  • Johny19
    Johny19 almost 2 years

    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)

  • Saurabh Verma
    Saurabh Verma almost 5 years
    @lacas final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
  • Sathish
    Sathish almost 4 years
    what is downloadId?
  • Windgate
    Windgate about 3 years
    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?