How to create dynamic WhatsApp Stickers Part 2 — Android

Vanshika Arora
Bobble Engineering
Published in
3 min readJan 16, 2019

This is the second part of the Tutorial, “How to add WhatsApp stickers dynamically”. If you haven’t gone through the first part I suggest you to go through it from here, before reading further.

Recap

In the previous part we learnt about

  • ContentProviders and it’s use in our context.
  • We have created a class that extends ContentProvider and in the onCreate we have added 4 URI’s to the Uri matcher object then returned the specific Uri in the overridden function getType().
  • Then in the query method of our ContentProvider we have returned specific cursor object (containing details of StickerPack and the Sticker) corresponding to each Uri.

Work Ahead

Now we will study about AssetFileDescriptor and see how these images are rendered to WhatsApp.

Where is AssetFileDescriptor used?

This provides your own opened FileDescriptor that can be used to read the data, as well as the offset and length of that entry’s data in the file.

And so here in our app we will be using AssetFileDescriptor to fetch the Image Assets using AssetManager. But before doing that we need to make sure that:

  • The Uri is correct
  • Stickers and StickPack Icon with the given file name exist in the storage.

And hence before fetching the image file we create a function to verify the above conditions and then call the function fetchFile() to fetch the image AssetFileDescriptor.

Here first we verify the uri with the pathSegments(). Then we loop through the StickerPack objects and Sticker objects (defined in the following gists StickerPack, Sticker) to see if the corresponding file exists. If the conditions are met then we call the fetchFile method.

In the fetchFile method we return an object of AssetFileDescriptor by calling the function openFd() on the AssetManager object and passing the file name as the parameter (in this case it is identifier + “/” + fileName)

You need to surround this with try/catch to handle IOException.

So, That’s All about ContentProviders.

You have achieved another Milestone. We have covered most of the things for creating Dynamic Stickers. Take a break to grasp everything covered so far and then begin with the next section.

Moving on

Things yet to be covered:

  • Scaling the images to the required dimensions and format
  • Sending Intent to WhatsApp

Let’s begin.

Scaling the images to the required dimensions and format

WhatsApp accepts only those Sticker Packs in which all the stickers meet these requirements(under Advanced development — — Content Provider).

To create a scaled Bitmap from your original Bitmap use (Beware that scaling may distort the image, so you might want to crop as well):

bitmap = Bitmap.createScaledBitmap(bitmap, 512, 512, true);

To convert the Bitmap to webp format with Quality of image=’quality’ and outputStream ‘out’ use:

bitmap.compress(Bitmap.CompressFormat.WEBP, 100, out);

Sending Intent to WhatsApp

Now comes the final task: Sending an Intent to WhatApp (with extra details like StickerPack name, identifier and ContentProvider authority) to add the Sticker Pack to WhatsApp. Let’s check the code for this

How to catch if an exception has occurred in this case?

If there arises an error in adding the sticker pack then in the onActivityResult we check the result code if it is cancelled then we can check the validation_error using the below code

Congratulations, You have Completed the Tutorial!!

Hope you have enjoyed reading the article and grabbed a lot from it . If you did then don’t forget to give claps on the article.

Happy Coding 😃

--

--

Vanshika Arora
Bobble Engineering

SDE Intern’19 @Microsoft | Mentor @Wikimedia | Android Developer | Blogger at MindOrks | Open source Contributor | Username(Github &LinkedIn)— vanshikaarora