首页 > 解决方案 > 由于错误代码,无法使用共享图像按钮:无法找到包含的已配置根

问题描述

我正在创建一个共享图像按钮,但由于某种原因,我收到了这样的错误代码:

java.lang.IllegalArgumentException: Failed to find configured root that contains /document/image:74
        at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744)
        at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
        at com.jawad.photoeditor.MainActivity$5.onClick(MainActivity.java:163)

我的清单看起来像:

<provider>
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
       <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/fileprovider" />
</provider>

我的fileprovider.xml看起来像:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="external_files"
        path="." />
    <external-files-path
        name="external_files"
        path="." />
    <cache-path
        name="cache"
        path="." />
    <external-cache-path
        name="external_cache"
        path="." />
    <files-path
        name="files"
        path="." />
</paths>

我的具体代码如下:

final Button shareButton = findViewById(R.id.shareButton);
        shareButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                File file = new File(imageUri.getPath());
                Intent intent = new Intent(Intent.ACTION_SEND);
                FileProvider.getUriForFile(Objects.requireNonNull(getApplicationContext()),
                        BuildConfig.APPLICATION_ID + ".provider", file);
                intent.setDataAndType(imageUri, "image/*");
                intent.putExtra(Intent.EXTRA_STREAM, imageUri);
                startActivity(intent);
            }
        });

试过这个,但它没有解决问题。任何帮助将不胜感激,在此先感谢!

标签: javaandroid

解决方案


所以我刚刚尝试在我的 xml 文件中添加根路径。

<root-path name="root" path="." /> 

它现在可以作为一种魅力。问题已解决


推荐阅读