找不到包含路径的已配置根目录

时间:2018-09-22 08:20:08

标签: android

执行时出现错误:

 @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中发现了问题,但是这些并不能解决我的问题。我认为这与之前提出的要求有所不同。

AndroidManifest:

  <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>

1 个答案:

答案 0 :(得分:0)

使用此:

N

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