I'm developing an android application for adding custom stickers to WhatsApp.On adding sticker to WhatsApp a error dailog is shown with message "There is some issue with this sticker pack and it cannot be added to Whatsap". The error I get onActivitResult is "fd should not be invalid". I can't track down the reason for this error.
This is my json file which i create in my project:
[
{
"identifier":"2",
"isWhitelisted":false,
"licenseAgreementWebsite":"",
"name":"Prank",
"privacyPolicyWebsite":"",
"publisher":"Bilal",
"publisherEmail":"",
"publisherWebsite":"",
"stickers":[
{
"emojis":[
],
"imageFileName":"0",
"size":0,
"uri":"/storage/emulated/0/Android/media/com.firebase.stickersload/Prank/image_0.webp"
},
{
"emojis":[
],
"imageFileName":"1",
"size":0,
"uri":"/storage/emulated/0/Android/media/com.firebase.stickersload/Prank/image_1.webp"
},
{
"emojis":[
],
"imageFileName":"2",
"size":0,
"uri":"/storage/emulated/0/Android/media/com.firebase.stickersload/Prank/image_2.webp"
},
{
"emojis":[
],
"imageFileName":"3",
"size":0,
"uri":"/storage/emulated/0/Android/media/com.firebase.stickersload/Prank/image_3.webp"
},
{
"emojis":[
],
"imageFileName":"4",
"size":0,
"uri":"/storage/emulated/0/Android/media/com.firebase.stickersload/Prank/image_4.webp"
}
],
"stickersAddedIndex":5,
"totalSize":0,
"trayImageFile":"trayimage",
"trayImageUri":"/storage/emulated/0/Android/media/com.firebase.stickersload/Prank/image_0.png"
}
]
Here is my contentProvider Method "openAssetFile" in which this method is occurring.
override fun openAssetFile(uri: Uri, mode: String): AssetFileDescriptor? {
val matchCode = MATCHER.match(uri)
Log.d("de_sticker", uri.toString() + " facffffff")
Log.w("de_sticker", uri.path + " uri path")
val pathSegments = uri.pathSegments
val csp: StickerPack? = StickerBook.getStickerPackByIdWithContext(
pathSegments[pathSegments.size - 2],
context
)
Log.d("de_sticker", "openAssetFile: id: ${pathSegments.size-2}")
if (csp != null) {
val filename = pathSegments[pathSegments.size - 1]
Log.d("de_sticker", "openAssetFile: path segments file name: ${filename}")
var pfd: ParcelFileDescriptor? = null
try {
if (filename == "trayimage" && csp.trayImageFile != null) {
pfd = Objects.requireNonNull(context)!!.contentResolver.openFileDescriptor(
csp.trayImageUri!!, "r"
)
Log.d("de_sticker", "openAssetFile: pdf is: ${pfd}")
Log.d(
"de_sticker",
java.lang.String.valueOf(csp.trayImageFile) + ""
)
} else {
try {
pfd = Objects.requireNonNull(context)!!.contentResolver.openFileDescriptor(
csp.getStickerById(Integer.valueOf(filename))!!.uri!!, "r"
)
Log.d("de_sticker", "openAssetFile: else condition $pfd")
Log.w(
"ASSETFILE ACTUAL URI", java.lang.String.valueOf(
csp.getStickerById(
Integer.valueOf(filename)
)!!.uri
) + ""
)
} catch (e: NullPointerException) {
Log.d(
"de_sticker",
"WhatsApp tried to access a non existent sticker, id: $filename"
)
}
}
}
catch (e: Exception) {
Log.d("de_sticker", "openAssetFile: ${e.message}")
e.printStackTrace()
}
Log.d("de_sticker", "openAssetFile: pfd is $pfd")
return AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH)
}
return getImageAsset(uri)
/*if (matchCode == STICKERS_ASSET_CODE || matchCode == STICKER_PACK_TRAY_ICON_CODE) {
return getImageAsset(uri);
}
return null;*/
}
Is there something i am missing here? Can somebody please help me out with this. Any help will be appreciated. Thank you.
Note: all the given uris(images) are placed in my mobile device and i allow all required permissions as well