关于android:Android-配置-FileProvider

36次阅读

共计 1382 个字符,预计需要花费 4 分钟才能阅读完成。

  1. 在我的项目 AndroidManifest.xml 增加配置项:
<provider
  android:name="androidx.core.content.FileProvider"
  android:authorities="${applicationId}.fileprovider"
  android:exported="false"
  android:grantUriPermissions="true"
  tools:replace="android:authorities">
  <meta-data
    android:name="android.support.FILE_PROVIDER_PATHS"
    android:resource="@xml/file_provider_paths"
    tools:replace="android:resource" />
</provider>
  1. res/xml 目录下增加文件file_provider_paths,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path name="external_files_path" path="." />
    <external-cache-path name="external_cache_path" path="." />
    <external-path name="external_path" path="." />
    <files-path name="files_path" path="." />
    <cache-path name="cache_path" path="." />
</paths>

这里须要理解了一下 Android 文件系统。

XML 代码 文件门路
<external-files-path name=”external_files_path” path=”.” /> Context.getExternalFilesDir(String) /storage/emulated/0/Android/data/<application package>/files
<external-cache-path name=”external_cache_path” path=”.” /> Context.getExternalCacheDir() /storage/emulated/0/Android/data/<application package>/cache
<external-path name=”external_path” path=”.” /> Environment.getExternalStorageDirectory() /storage/emulated/0
<files-path name=”files_path” path=”.” /> Context.getFilesDir() /data/data/<application package>/files
<cache-path name=”cache_path” path=”.” /> Context.getCacheDir() /data/data/<application package>/cache

正文完
 0