I have integrated Snapchat's Creative Kit in my Android app. After processing, I receive an image from the server in the form of Byte Array which I am saving to the disk and then sending the file to the Snapchat's Creative Kit as shown below.
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)
}
}
I have also added provider
in the manifest file as shown below:
<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>
And in the provider_paths_app.xml
, I have tried all the possible paths by referring this answer and none of them works.
<?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>
With the above path, I am getting the below error.
Couldn't find meta-data for provider with authority my.package.name.fileprovider
All I have to do is send this image to Snapchat but I am unable to figure out what I am doing wrong. Any help will be appreciated.
mImageFromCamera = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileprovider", mImageFile); android:authorities="${applicationId}.fileprovider"
authorities must be same in xml and in code
I just removed the '$' from android:authorities="${applicationId}.provider" and it works like a charm now.
This is my provider declaration, the value of ${application} is "com.limxtop.research", make sure that the name of authorities is the same with that of the codes below.
So, maybe your codes post here is not complete, there you should pass "my.package.name.fileprovider" as parameter some where.
Change the authorities to a unique name to solve the issue like
android:authorities="${applicationId}.myUniquefileprovider"
also in java code
First write the following tag in manifest under the Tag
Then Create a xml folder in res and create a file names: provide+paths.xml and then copy paste the code:
and now most developer mistake in programs to create File in program then we will use:
Hope this will work for yor !!!