如何在 Android 应用中设置 `android:authorities`

在 Android 应用中设置 android:authorities 属性主要是为 ContentProviderFileProvider 定义的,以确保这些提供者可以在应用之间或应用内部被唯一地识别。以下是如何为 FileProvider 设置 android:authorities 的步骤:

  1. 定义 FileProvider

    在你的 AndroidManifest.xml 文件中,在 <application> 标签内添加 <provider> 标签来定义 FileProvider

  2. 设置 android:name

    android:name 属性设置为 androidx.core.content.FileProvider(如果你使用的是 AndroidX 库)或者 android.support.v4.content.FileProvider(如果你使用的是 Support Library)。

  3. 设置 android:authorities

    android:authorities 属性提供一个唯一的字符串值。通常这个值会基于你的应用的包名,并加上一个自定义的后缀。例如,如果你的包名是 com.example.myapp,那么你可以将 android:authorities 设置为 com.example.myapp.provider

  4. 设置 android:exportedandroid:grantUriPermissions

    对于 FileProvider,通常将 android:exported 设置为 false(除非你有特殊需求),并将 android:grantUriPermissions 设置为 true 以允许接收 Intent 的应用访问由 FileProvider 提供的文件。

  5. 添加 meta-data

    <provider> 标签内添加一个 <meta-data> 标签,用于指定 FileProvider 可以访问的文件路径的 XML 资源文件。

以下是一个完整的示例:

复制代码

xml复制代码

|---|-------------------------------------------------------------------------|
| | <manifest xmlns:android="http://schemas.android.com/apk/res/android" |
| | package="com.example.myapp"> |
| | |
| | <application |
| | ... > |
| | |
| | <!-- 其他组件 --> |
| | |
| | <provider |
| | android:name="androidx.core.content.FileProvider" |
| | android:authorities="com.example.myapp.provider" |
| | android:exported="false" |
| | android:grantUriPermissions="true"> |
| | <meta-data |
| | android:name="android.support.FILE_PROVIDER_PATHS" |
| | android:resource="@xml/file_paths" /> |
| | </provider> |
| | |
| | <!-- 其他组件 --> |
| | |
| | </application> |
| | |
| | </manifest> |

在这个示例中,@xml/file_paths 是指向 res/xml/file_paths.xml 文件的引用,该文件应该包含你想要通过 FileProvider 共享的文件的路径。

file_paths.xml 的内容可能如下所示:

复制代码

xml复制代码

|---|-------------------------------------------------------------------------|
| | <paths xmlns:android="http://schemas.android.com/apk/res/android"> |
| | <files-path name="my_images" path="images/"/> |
| | <external-files-path name="external_images" path="Pictures/MyApp/"/> |
| | <!-- 其他路径定义 --> |
| | </paths> |

这个示例定义了两个路径:一个是应用内部文件目录中的 images/ 子目录,另一个是外部存储中的 Pictures/MyApp/ 目录。你可以根据你的需求添加或修改这些路径。

相关推荐
TeleostNaCl8 天前
如何安装 Google 通用的驱动以便使用 ADB 和 Fastboot 调试(Bootloader)设备
android·经验分享·adb·android studio·android-studio·android runtime
我命由我123458 天前
Android 开发 - 一些画板第三方库(DrawBoard、FingerPaintView、PaletteLib)
android·java·java-ee·android studio·安卓·android-studio·android runtime
amy_jork16 天前
android证书相关
android-studio
我命由我123451 个月前
Android 开发问题:The specified child already has a parent.
android·java·开发语言·java-ee·android jetpack·android-studio·android runtime
尘云逸1 个月前
将开发的软件安装到手机:环境配置、android studio设置、命令行操作
android·react native·adb·智能手机·gradle·android studio·android-studio
我命由我123452 个月前
Android Studio 提示信息 ‘equals(““)‘ can be replaced with ‘isEmpty()‘
android·ide·android studio·安卓·android jetpack·android-studio·android runtime
Dyan_csdn2 个月前
AndroidStudio环境搭建
android·android-studio
Mryan20053 个月前
Android 应用多语言与系统语言偏好设置指南
android·java·国际化·android-studio·多语言
我命由我123453 个月前
Android 开发问题:CardView 的阴影效果会受到父容器的裁切
android·java·开发语言·java-ee·android studio·android-studio·android runtime
帅次3 个月前
快速使用 Flutter 中的 SnackBar 和 Toast
android·flutter·ios·小程序·kotlin·iphone·android-studio