【错误记录】Android 文件分享 FileProvider 设置错误

2023-03-29 12:40:23 浏览数 (2)

文章目录

  • 一、报错信息
  • 二、解决方案

一、报错信息


分享内部存储到文件到其它应用 ;

代码语言:javascript复制
2021-05-18 16:10:23.480 31881-31881/kim.hsl.file E/DEBUG: --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
2021-05-18 16:10:23.480 31881-31881/kim.hsl.file E/DEBUG: Process Name: 'kim.hsl.file'
2021-05-18 16:10:23.480 31881-31881/kim.hsl.file E/DEBUG: Thread Name: 'main'
2021-05-18 16:10:23.480 31881-31881/kim.hsl.file E/DEBUG: Back traces starts.
2021-05-18 16:10:23.481 31881-31881/kim.hsl.file E/DEBUG: java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/kim.hsl.file/files/5.mp3
2021-05-18 16:10:23.481 31881-31881/kim.hsl.file E/DEBUG:     at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711)
2021-05-18 16:10:23.482 31881-31881/kim.hsl.file E/DEBUG:     at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
2021-05-18 16:10:23.482 31881-31881/kim.hsl.file E/DEBUG:     at android.view.View.performClick(View.java:6597)
2021-05-18 16:10:23.482 31881-31881/kim.hsl.file E/DEBUG:     at android.view.View.performClickInternal(View.java:6574)
2021-05-18 16:10:23.483 31881-31881/kim.hsl.file E/DEBUG:     at android.view.View.access$3100(View.java:778)
2021-05-18 16:10:23.483 31881-31881/kim.hsl.file E/DEBUG:     at android.view.View$PerformClick.run(View.java:25885)
2021-05-18 16:10:23.483 31881-31881/kim.hsl.file E/DEBUG:     at android.os.Handler.handleCallback(Handler.java:873)
2021-05-18 16:10:23.484 31881-31881/kim.hsl.file E/DEBUG:     at android.os.Handler.dispatchMessage(Handler.java:99)
2021-05-18 16:10:23.484 31881-31881/kim.hsl.file E/DEBUG:     at android.os.Looper.loop(Looper.java:193)
2021-05-18 16:10:23.484 31881-31881/kim.hsl.file E/DEBUG:     at android.app.ActivityThread.main(ActivityThread.java:6718)
2021-05-18 16:10:23.484 31881-31881/kim.hsl.file E/DEBUG:     at java.lang.reflect.Method.invoke(Native Method)
2021-05-18 16:10:23.485 31881-31881/kim.hsl.file E/DEBUG:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
2021-05-18 16:10:23.485 31881-31881/kim.hsl.file E/DEBUG:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2021-05-18 16:10:23.485 31881-31881/kim.hsl.file E/DEBUG: Back traces ends.
2021-05-18 16:10:23.485 31881-31881/kim.hsl.file E/DEBUG: --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

二、解决方案


配置 FileProvider ,

使用 androidx 支持库的情况 :

代码语言:javascript复制
<manifest>
    ...
    <application>
        ...
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.mydomain.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
        ...
    </application>
</manifest>

或 使用 support 支持库的情况 :

代码语言:javascript复制
<manifest>
    ...
    <application>
        ...
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.mydomain.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
        ...
    </application>
</manifest>

file_paths.xml 文件配置 : 下面的配置将所有的文件设置为可分享的文件 , 具体还要根据需求自己定制 ;

代码语言:javascript复制
<paths 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <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="." />
</paths>

0 人点赞