从DownloadManager访问下载的文件

问题描述 投票:0回答:1

我正在使用DownloadManager下载新版本的APK并安装它(不能使用谷歌播放)。

从其他问题我了解到从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)

提前致谢!

android android-download-manager android-fileprovider
1个回答
0
投票

在filepaths.xml中更改files-path to external-path使其工作

来自:answer

© www.soinside.com 2019 - 2024. All rights reserved.