Search code examples
androidandroid-contentproviderandroid-fileandroid-fileprovider

Fileprovider Failed to find configured root error


I am creating a app in which people can organise their media in different *let's say files. So i store the absolute paths of media because i want to keep them stored sorted for a long time. I was using a fileprovider to make it possible to share these files otherwise you get a FileUriExposed error. It worked fine on my phone, but i switched to samsung phone for some reason it started to give me this error

Process: com.nisarg.locsav, PID: 9125
    java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/6363-3562/DCIM/Camera/20200720_193629.jpg
        at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744)
        at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
        at com.nisarg.locsav.ImageFragment.onCreateView(ImageFragment.java:35)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
        at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:447)
        at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2169)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1992)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947)
        at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1818)
        at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:303)
        at androidx.fragment.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:270)
        at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1244)
        at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092)
        at androidx.viewpager.widget.ViewPager.onMeasure(ViewPager.java:1622)
        at android.view.View.measure(View.java:24962)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
        at android.view.View.measure(View.java:24962)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7149)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
        at android.view.View.measure(View.java:24962)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7149)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:403)
        at android.view.View.measure(View.java:24962)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7149)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:24962)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7149)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
        at android.view.View.measure(View.java:24962)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7149)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:1018)
        at android.view.View.measure(View.java:24962)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3353)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:2082)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2381)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1942)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8595)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:988)
        at android.view.Choreographer.doCallbacks(Choreographer.java:765)
        at android.view.Choreographer.doFrame(Choreographer.java:700)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:967)
        at android.os.Handler.handleCallback(Handler.java:873)
2020-07-21 11:56:53.820 9125-9125/com.nisarg.locsav E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7156)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)

I researched a bit and found out that i needed to add all file-path elements to my paths for file provider and so i did as follows

Manifest

         <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.nisarg.locsav"
            android:grantUriPermissions="true"
            tools:replace="android:authorities">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"
                tools:replace="android:resource"/>
        </provider>

and @xml/provider_paths

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="external" 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="." />
    <external-media-path name="external_media" path="."/>
</paths>

Here is where I am trying to access the fileprovider uri


                            Intent ity= new Intent();
                            ity.setAction(Intent.ACTION_SEND_MULTIPLE);
                            ity.setType("image/*");
                            ArrayList<Uri> uris=new ArrayList<Uri>();
                            for(photo_n pl:photos)
                            {
                                uris.add(FileProvider.getUriForFile(getActivity(),BuildConfig.APPLICATION_ID,new File(pl.photUri)));
                            }
                            ity.putParcelableArrayListExtra(Intent.EXTRA_STREAM,uris);
                            startActivity(ity);
                        }

I am still getting the above error, what do you think I am doing wrong? What is the correct method?


Solution

  • Failed to find configured root that contains /storage/6363-3562/DCIM/Camera/20200720_193629.jpg

    FileProvider cannot serve files from removable micro SD cards.