我已将 Snapchat 的 Creative Kit 集成到我的 Android 应用程序中。处理后,我从服务器接收到一个字节数组形式的图像,我将其保存到磁盘,然后将文件发送到 Snapchat 的创意工具包,如下所示。
private fun downloadImage(
fileName: String,
imageByteArray: ByteArray?): Uri? {
val state = Environment.getExternalStorageState()
if (Environment.MEDIA_MOUNTED == state) {
val downloadDir = File(
Environment.getExternalStorageDirectory(), context?.getString(R.string.app_name)
)
if (!downloadDir.isDirectory) {
downloadDir.mkdirs()
}
val file = File(downloadDir, fileName)
var ostream: FileOutputStream? = null
try {
ostream = FileOutputStream(file)
ostream.write(imageByteArray)
ostream.flush()
ostream.close()
}
} catch (e: IOException) {
e.printStackTrace()
}
val snapCreativeKitApi = SnapCreative.getApi(context!!)
val snapMediaFactory = SnapCreative.getMediaFactory(context!!)
lateinit var snapPhotoFile: SnapPhotoFile
try {
snapPhotoFile = snapMediaFactory.getSnapPhotoFromFile(file)
} catch (e: SnapMediaSizeException) {
return
}
val snapPhotoContent = SnapPhotoContent(snapPhotoFile)
snapCreativeKitApi.send(snapPhotoContent)
}
}
我还在清单文件中添加了 provider
,如下所示:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths_app" />
</provider>
在 provider_paths_app.xml
中,我通过引用 这个 答案尝试了所有可能的路径,但它们都不起作用。
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="My App Name"
path="." />
</paths>
使用上述路径,我收到以下错误。
Couldn't find meta-data for provider with authority my.package.name.fileprovider
我所要做的就是将此图像发送到 Snapchat,但我无法弄清楚我做错了什么。任何帮助将不胜感激。
原文由 Mehul Kanzariya 发布,翻译遵循 CC BY-SA 4.0 许可协议
首先,在清单中的
<application>
标签下写入以下标签然后在
res
中创建一个xml文件夹并创建一个名为:provider_paths.xml的文件,然后复制粘贴代码:现在这是大多数开发人员犯错误的地方:在您的脚本中,使用以下命令创建文件: