英文:
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
这是我加载图像以供参考的方式。
英文:
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
<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>
And my file_paths.xml, I have all of these entries so I could try and debug it properly
<?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>
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(".") + 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"));
}
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
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论