我已经在我的Android应用程序中集成了Snapchat的Creative Kit。处理后,我从服务器收到了字节数组形式的图像,将其保存到磁盘,然后将文件发送到Snapchat的Creative Kit,如下所示。
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)
}
}
我还在 list 文件中添加了
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
中,我通过引用this答案尝试了所有可能的路径,但没有一个起作用。<?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,但我无法弄清楚自己在做什么错。任何帮助将不胜感激。
最佳答案
首先在标签下的 list 中写入以下标签
<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" />
</provider>
然后在res中创建一个xml文件夹并创建一个文件名:Provide + paths.xml
然后复制粘贴代码:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external_files"
path="." />
</paths>
现在大多数开发人员在程序中错误地在程序中创建File,那么我们将使用:
FileProvider.getUriForFile(Objects.requireNonNull(getApplicationContext()),
BuildConfig.APPLICATION_ID + ".provider", file);
希望这对您有用!