【问题标题】:Access a downloaded file from DownloadManager从 DownloadManager 访问下载的文件
【发布时间】:2019-02-13 08:57:52
【问题描述】:

我正在使用DownloadManager 下载新版本的 APK 并安装(无法使用 Google Play)。

从其他问题中我了解到,从 Android 7 开始,我 需要使用FileProvider 来启动apk,但出现错误:

未能找到配置的根目录包含

下载和接收代码:

private void startDownload3(String url) {

    final DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setTitle(getString(R.string.downloading_update_title));
    String filename = ((int)(Math.random()*100))+getString(R.string.update_file_name_base);
    Logger.e("downloading file = "+filename );
    request.setVisibleInDownloadsUi(true);
    request.setDestinationInExternalPublicDir("/Download", filename);
    saveFileName(filename);

    final long enqueue = dm.enqueue(request);

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                openFile();
            }
        }
    };

    registerReceiver(receiver,
            new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}

private void openFile() {
    File[] files = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).listFiles();
    Logger.e("folder Size: "+ files.length);
    String fileName = getFileName(this);
    File realFile=null;
    for (int i = 0; i < files.length; i++)
    { //for testing purposes
        if (fileName.equals(files[i].getName()))
            realFile=files[i];
        Logger.e("file names :  "+ files[i].getName());
    }
    if(realFile!=null) {
        Intent install;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //android 7+
            Uri apkUri = FileProvider.getUriForFile(MyFirebaseMessagingService.this, "mmn.policy.Visitors2.fileprovider", realFile);


            install = new Intent(Intent.ACTION_INSTALL_PACKAGE);
            install.setData(apkUri);
            install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(install);
        } else {                                            //todo  TEST THIS android 6-
            Uri apkUri = Uri.fromFile(realFile);
            install = new Intent(Intent.ACTION_VIEW);
            install.setDataAndType(apkUri, "application/vnd.android.package-archive");
            install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(install);
        }
    }
}

还有filepaths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
     <files-path name="Download" path="Download/" />
</paths>

在清单中

<provider
    android:name=".GenericFileProvider"
    android:authorities="com.example.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/filepaths" />
</provider>

现在我在我所做的测试中看到了该文件,但 FileProvider 失败了。有没有办法让FileProvider 指向与setDestinationInExternalPublicDir 相同的地方?

完整的logcat:

Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Download/54test.apk
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:712)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:401)
at com.example.MyFirebaseMessagingService.openFile(MyFirebaseMessagingService.java:310)
at com.example.MyFirebaseMessagingService.access$000(MyFirebaseMessagingService.java:49)
at com.example.MyFirebaseMessagingService$3.onReceive(MyFirebaseMessagingService.java:276)

【问题讨论】:

  • 请发布详细的错误日志
  • @TejasPandya 添加 logcat 错误
  • this 可能会有所帮助

标签: android android-download-manager android-fileprovider


【解决方案1】:

在 filepaths.xml 中将文件路径更改为外部路径使其工作

来自:answer

【讨论】:

    猜你喜欢
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多