首页 > 解决方案 > 找不到包含路径的已配置根目录

问题描述

执行时出现错误:

 @SuppressLint("SetWorldReadable")
    public void installAPK(File dest) {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            dest.setReadable(true, false);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(Uri.fromFile(dest), "application/vnd.android.package-archive");
            this.getApplicationContext().startActivity(intent);
        } else {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            Uri fileUri = FileProvider.getUriForFile(this,
                    "com.developerfromjokela.fileprovider",
                    dest);

            intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(intent);
        }
    }

我得到:

09-22 11:11:12.866 10506-10551/com.developerfromjokela.pusahub E/AndroidRuntime: FATAL EXCEPTION: Thread-5
    Process: com.developerfromjokela.pusahub, PID: 10506
    java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/PusaHub/PusaStore/.downloads/PusaCloud_20180922_111100.apk
        at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:738)
        at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:417)
        at com.developerfromjokela.pusahub.AppDetailsActivity.installAPK(AppDetailsActivity.java:412)
        at com.developerfromjokela.pusahub.AppDetailsActivity.lambda$downloadFile$3$AppDetailsActivity(AppDetailsActivity.java:329)
        at com.developerfromjokela.pusahub.AppDetailsActivity$$Lambda$1.run(Unknown Source:30)
        at java.lang.Thread.run(Thread.java:764)

我很早就在stackoverflow中搜索并发现了问题,但这些并不能解决我的问题。我认为这与之前提出的问题有所不同。

安卓清单:

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

    </provider>

文件路径 XML:

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

标签: android

解决方案


用这个:

对于N版本

File file = new File(filePathUri.getPath());
Uri photoUri = FileProvider.getUriForFile(getActivity(),getActivity().getPackageName() + ".provider", file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);

推荐阅读