【问题标题】:How to download and save Bitmap of animated webp?如何下载和保存动画 webp 的位图?
【发布时间】:2022-01-27 20:10:15
【问题描述】:

当我为动画贴纸包使用 webp 文件时,它会被拒绝,但如果对静态贴纸使用相同的文件,它会被排除在外。在查看了所有代码后,我才知道这是这些文件出现问题的最后一点。但不知道如何识别webp文件保存后是否保持动画webp。请分享您的想法。

ps:我将这些 webp 文件用于 whatsapp 贴纸包。有标志“animated_sticker_pack”。我们必须告诉whatsapp,这个包只包含带有适当格式的动画webp。如果我将其设置为 false,则添加贴纸包(让它成为静态或动画 webp)。但是,如果我将该标志设置为 true,那么那些动画 webp 将因包显示错误而被拒绝,此包有问题...。因此,帧数可能小于所需的帧数。它被接受为静态意味着它可能只有单帧。 为避免有关文件类型、格式、大小以及我使用 WhatsApp 示例应用程序中的示例文件的所有问题

代码:

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);
}

【问题讨论】:

  • using webp files for animated sticker packs it get rejected 在哪里?通过谁?什么时候?这是关于什么的?
  • 不清楚你想要什么。您是否在保存前编辑位图?或者你想完成什么?
  • 你能不能问一下:如何下载和保存动画webp文件?
  • @blackapps 对此感到抱歉。我现在已经编辑了这个问题。请看一看。
  • 那是一个java文件。我该怎么办?再次:直接下载并保存到文件。不要使用中间位图。

标签: java android bitmap webp


【解决方案1】:

你可以下载并保存在doInBackground()中

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

    FileOutputStream out = new FileOutputStream(file);

然后创建一个循环,从输入流中读取缓冲区中的字节并写入输出流。

完成后不要忘记关闭所有流。

【讨论】:

  • 谢谢。我无法实现“在缓冲区中读取字节”。尝试了不同的不同方法。这需要时间,但最终完成了。非常感谢..
猜你喜欢
  • 2018-09-22
  • 1970-01-01
  • 2019-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多