Android Java, FileProviders, getUriFromFile failed with error Failed to find configured root that contains /storage/1018-2710/Pictures/Sarx7IIJi-o.jpg

huangapple go评论67阅读模式
英文:

Android Java, FileProviders, getUriFromFile failed with error Failed to find configured root that contains /storage/1018-2710/Pictures/Sarx7IIJi-o.jpg

问题

以下是您提供的内容的翻译部分:

所以,我一直在尝试通过电子邮件(或任何应用程序)发送我的图像。我使用MediaStore API将图像加载到我的应用程序中。我有它们的绝对路径 /storage/1018-2710/Pictures/Sarx7IIJi-o.jpg,但是当我尝试从它们获取Uri时,我收到一个错误,上面写着 无法找到包含 /storage/1018-2710/Pictures/Sarx7IIJi-o.jpg 的已配置根目录

这是我的 AppManifest:

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

以及我的 file_paths.xml 文件,我拥有所有这些条目,以便我可以正确调试它:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="external"
        path="." />
    <external-files-path
        name="external_files"
        path="." />
    <external-files-path
        name="external_files_pictures"
        path="storage/1018-2710/Pictures/" />
    <cache-path
        name="cache"
        path="." />
    <external-cache-path
        name="external_cache"
        path="." />
    <files-path
        name="files"
        path="." />
    <external-media-path name="media" path="." />
    <files-path name="my_images" path="images/"/>
</paths>

这是我尝试获取 URI 的地方:

  {
           File myFile = new File(mViewModel.selectedImages.get(0).getmImageUrl());
           MimeTypeMap mime = MimeTypeMap.getSingleton();
           String ext = myFile.getName().substring(myFile.getName().lastIndexOf(".") + 1);
           String type = mime.getMimeTypeFromExtension(ext);
           Intent sharingIntent = new Intent("android.intent.action.SEND");
           sharingIntent.setType(type);
           Uri contentUri = getUriForFile(requireContext(), "com.nikolam.fileprovider", myFile);
           sharingIntent.putExtra("android.intent.extra.STREAM", contentUri);
           startActivity(Intent.createChooser(sharingIntent, "Share using"));
}

我尝试了许多不同的路径和配置,但迄今为止都没有成功。

这是有关项目的链接:https://github.com/Nikola-Milovic/GalleryClone

这是我加载图像以供参考的方式

这是其中一张图片的Android Java, FileProviders, getUriFromFile failed with error Failed to find configured root that contains /storage/1018-2710/Pictures/Sarx7IIJi-o.jpg

英文:

So I've been trying to send my Image via the email (or whatever app). I load the images into my app using the MediaStore API. I have their absolute path /storage/1018-2710/Pictures/Sarx7IIJi-o.jpg, but when I try to get Uri from them I receive an error saying Failed to find configured root that contains /storage/1018-2710/Pictures/Sarx7IIJi-o.jpg

This is my AppManifest

    &lt;provider
            android:name=&quot;androidx.core.content.FileProvider&quot;
            android:authorities=&quot;com.nikolam.fileprovider&quot;
            android:exported=&quot;false&quot;
            android:grantUriPermissions=&quot;true&quot;&gt;
            &lt;meta-data
                android:name=&quot;android.support.FILE_PROVIDER_PATHS&quot;
                android:resource=&quot;@xml/file_paths&quot; /&gt;
        &lt;/provider&gt;

And my file_paths.xml, I have all of these entries so I could try and debug it properly

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;paths&gt;
    &lt;external-path
        name=&quot;external&quot;
        path=&quot;.&quot; /&gt;
    &lt;external-files-path
        name=&quot;external_files&quot;
        path=&quot;.&quot; /&gt;
    &lt;external-files-path
        name=&quot;external_files_pictures&quot;
        path=&quot;storage/1018-2710/Pictures/&quot; /&gt;
    &lt;cache-path
        name=&quot;cache&quot;
        path=&quot;.&quot; /&gt;
    &lt;external-cache-path
        name=&quot;external_cache&quot;
        path=&quot;.&quot; /&gt;
    &lt;files-path
        name=&quot;files&quot;
        path=&quot;.&quot; /&gt;
    &lt;external-media-path name=&quot;media&quot; path=&quot;.&quot; /&gt;
    &lt;files-path name=&quot;my_images&quot; path=&quot;images/&quot;/&gt;
&lt;/paths&gt;

And this is where I try to get the URI

  {
           File myFile = new File(mViewModel.selectedImages.get(0).getmImageUrl());
           MimeTypeMap mime = MimeTypeMap.getSingleton();
           String ext = myFile.getName().substring(myFile.getName().lastIndexOf(&quot;.&quot;) + 1);
           String type = mime.getMimeTypeFromExtension(ext);
           Intent sharingIntent = new Intent(&quot;android.intent.action.SEND&quot;);
           sharingIntent.setType(type);
           Uri contentUri = getUriForFile(requireContext(), &quot;com.nikolam.fileprovider&quot;, myFile);
           sharingIntent.putExtra(&quot;android.intent.extra.STREAM&quot;, contentUri);
           startActivity(Intent.createChooser(sharingIntent, &quot;Share using&quot;));
}

I tried many different paths and configurations but none have worked so far.

And this is the project in question https://github.com/Nikola-Milovic/GalleryClone

This is how I load images for reference

This is one of the images Android Java, FileProviders, getUriFromFile failed with error Failed to find configured root that contains /storage/1018-2710/Pictures/Sarx7IIJi-o.jpg

答案1

得分: 2

你的路径表明该文件位于可移动的 micro SD 卡上。

遗憾的是,FileProvider 无法从可移动媒体中提供文件。

你需要为此编写自己的 ContentProvider。

英文:
/storage/1018-2710/Pictures/Sarx7IIJi-o.jpg

Your path indicates that the file is on a removable micro sd card.

Sadly FileProvider cannot serve files from removable media.

You have to write your own ContentProvider for this.

huangapple
  • 本文由 发表于 2020年10月3日 17:36:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64182753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定