Search code examples
javaandroidandroid-contentresolverandroid-fileproviderandroid-storage

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

    <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

This is one of the images enter image description here


Solution

  • /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.