2

When I am using webp files for animated sticker packs it get rejected but if use same files for static stickers it get excepted. After looking all the codes I came to know that this is the last point where those files becomes problematic. But don not know how to identify if webp files stays as animated webp after saving. Please share your thought.

ps: I am using these webp files for whatsapp sticker packs. there is flag "animated_sticker_pack". we have to tell whatsapp that this pack contains only animated webp with proper fomrat. If I set it false then sticker pack get added (let it be static or animated webp). But if I set that flag true then those animated webp get rejected for pack showing error that There's problem with this pack.... So it might be that frames are lesser then it required. It get accepted as static means it might have single frame only. To avoid issues regarding file type,format,size and all I am using the sample files from WhatsApp sample app

Code:

public static void SaveImage(Bitmap finalBitmap, String name, String identifier) {

        String root = path + "/" + identifier;
        File myDir = new File(root);
        myDir.mkdirs();

        String fname = name;
        File file = new File(myDir, fname);
        
        if (file.exists()){
            file.delete();
        }
        
        try {

            // FileOutputStream 
            FileOutputStream out = new FileOutputStream(file);

            // Bitmap.compress
            finalBitmap.compress(Bitmap.CompressFormat.WEBP, 100, out); 
            
            // close
            out.flush();
            out.close();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
 }

////////////////////Other methods before saving images

private Bitmap downloadImageBitmap(String sUrl, String sIdentifier, String sName) {
    imageFileName = getLastBitFromUrl(sUrl).replace(".png", ".webp");
    identifier = sIdentifier;
    name = sName;
    Bitmap bitmap = null;
    try {
        InputStream inputStream = new URL(sUrl).openStream(); // Download Image from URL
        bitmap = BitmapFactory.decodeStream(inputStream); // Decode Bitmap
        inputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
     return bitmap;
}

@Override
protected Bitmap doInBackground(String... params) {
    return downloadImageBitmap(params[0], params[1], params[2]);
}

protected void onPostExecute(Bitmap result) {
    SaveImage(result, imageFileName, identifier);
}
CrackerKSR
  • 1,380
  • 1
  • 11
  • 29
  • `using webp files for animated sticker packs it get rejected` Where? By whom? When? What is this about? – blackapps Dec 29 '21 at 12:50
  • It is unclear what you want. Are you editing the bitmap before saving? Or what is it that you want to accomplish? – blackapps Dec 29 '21 at 12:53
  • Could not you better ask: How to download and save animated webp file? – blackapps Dec 29 '21 at 12:53
  • @blackapps sorry about that. I have edited the question now. please have a look. – CrackerKSR Dec 29 '21 at 12:56
  • It is Whatsapp sticker pack. as static sticker it get accepted but for animated sticker those webp get rejected. – CrackerKSR Dec 29 '21 at 12:57
  • I still dont understand what you want and you did not react on my suggestion that you just wanted to download and save a webp file. If so then just download and save without using an intermediate bitmap. You did not at all explain why you are using a bitmap. – blackapps Dec 29 '21 at 13:01
  • Yes I want to download and save webp but dont want to compress it. Here is the link for whole file. https://github.com/CrackerKSR/temporary/blob/main/RNWhatsAppStickersModule.java. – CrackerKSR Dec 29 '21 at 13:11
  • 1
    That is a java file. What should i do with it? Again: Download and save to file directly. Dont use an intermediate bitmap. – blackapps Dec 29 '21 at 13:18

1 Answers1

1

You can download and save in doInBackground()

    InputStream inputStream = new URL(sUrl).openStream(); // Download Image from URL

    FileOutputStream out = new FileOutputStream(file);

Then make a loop where you read bytes in a buffer from input stream and write to output stream.

Don't forget to close all streams when done.

blackapps
  • 8,011
  • 2
  • 11
  • 25
  • Thank you . I could not implement that "read byte in a buffer". Tried different different approach. It took time but finally done. Thank you so much.. – CrackerKSR Jan 02 '22 at 13:30