Fileprovider Failed to find configured root 错误

Fileprovider Failed to find configured root error

我正在创建一个应用程序,人们可以在其中组织他们的媒体到不同的 *比方说文件。所以我存储了媒体的绝对路径,因为我想让它们存储很长时间。我正在使用一个文件提供者来共享这些文件,否则你会得到一个 FileUriExposed 错误。它在我的 phone 上运行良好,但由于某种原因我切换到三星 phone 它开始给我这个错误

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)

我研究了一下,发现我需要将所有文件路径元素添加到文件提供程序的路径中,所以我做了如下操作

清单

         <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>

@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>

这是我尝试访问文件提供程序 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);
                        }

我仍然收到上述错误,您认为我做错了什么?正确的方法是什么?

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

FileProvider 无法提供可移动微型 SD 卡中的文件。

如 blackapps 所述,fileprovider 不提供可移动微型 SD 卡。所以我的代码失败了。但是我找到了解决方法,对于任何感兴趣的人,我都将其发布在这里。 将此添加到您的 @xml/provider_paths-

<root-path name="external_files" path="/storage/" />