android – 使用FileProvider时没有找到处理Intent的Activity

在我的应用程序中,我有自定义自动下载和安装APK它的工作原理如下
// 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清单:

<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(很抱歉由于某种原因因此削减了路径标记)

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

文件完成下载时,会调用onComplete,但是活动不会启动:

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 }

使用普通文件时://确实有效

使用文件提供程序时是否有一些我缺少的东西?活动是否因为找不到文件而无法启动?
我需要额外的许可吗? (目前我在外部存储上有INTERNET,READ和WRITE)

解决方法

软件包安装程序仅支持从Android 7.0开始的内容方案.在此之前 – 尽管有相反的文档 – 包安装程序仅支持文件方案.

根据您是否在Android 7.0上运行,您需要以不同的方式在您的Intent上设置Uri,例如在Build.VERSION.SDK_INT上进行分支.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...