将 Firebase 和 FileProvider 与构建变体结合使用

Sum*_*ain 5 android firebase android-fileprovider

我正在使用 Firebase 的构建变体,我的应用程序还需要使用 FileProvider 来共享要在 3rd 方应用程序中打开的应用程序文件。但是,当我尝试在 Intent 中发送 URI 时,应用程序崩溃并出现以下错误。

error:java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.debug/files/Pictures/JPEG_20170919_113955_102027655.jpg

这是我的配置 String authority = getString(R.string.authority_provider); Uri fileUri = FileProvider.getUriForFile(context, authority, file);

  • 将文件提供程序添加到 AndroidManifest.xml 中的应用程序标签内:

    <provider android:name="android.support.v4.content.FileProvider" android:authorities="@string/authority_provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>

  • file_paths.xml

    <?xml version="1.0" encoding="utf-8"?> <paths> <external-path name="my_images" path="Android/data/${applicationId}/files/Pictures" /> <external-file-path name="my_images" path="Android/data/${applicationId}/files/Pictures" /> </paths>

由于Firebase要求使用 2 个应用程序 ID 来保存崩溃日志,Firebase 控制台中的分析是分开的

  • 字符串:authority_provider - 添加到 build.gradle(app) 中:

    buildTypes { release { --- applicationIdSuffix "com.example" resValue "string", "authority_provider", "\"com.example.fileprovider\"" --- } debug { --- applicationIdSuffix '.debug' resValue "string", "authority_provider", "\"com.example.debug.fileprovider\"" --- } }

  • 文件:并将文件创建到 ExternalFilesDir 并将下载的数据保存到其中并使用相同的文件对象获取 URI。

    File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES); File file = File.createTempFile(fileName, fileExtension, storageDir);

我尝试了以下解决方案来解决但对我不起作用:

也关注了这篇博文。

归档时间:

查看次数:

780 次

最近记录:

7 年,11 月 前