Android无需FFmpeg将Bitmap列表转换为适配WhatsApp的动态WebP求助
动态WebP WhatsApp贴纸编码问题解决方案
报错原因
你遇到的Not supported FourCC: I.C.C.P.异常,是因为输入的GIF文件携带了ICC颜色配置元数据块,你使用的第三方WebP编码器暂不支持解析该类元数据块。
无FFmpeg依赖的实现方案
方案1:直接从Bitmap列表编码动态WebP(推荐,跳过GIF中转)
优先不经过GIF转码,避免额外元数据带来的兼容问题,根据你的目标系统版本选择对应实现:
- 若应用minSdk >= 31,可直接使用Android系统原生
AnimatedWebpEncoder实现,示例代码如下:
public boolean encodeAnimatedWebp(List<Bitmap> bitmaps, int frameDurationMs, String outputPath) { if (bitmaps == null || bitmaps.isEmpty()) { throw new IllegalArgumentException("Bitmap列表不能为空"); } // 校验符合WhatsApp贴纸要求:统一为512x512,带透明度 for (Bitmap bmp : bitmaps) { if (bmp.getWidth() != 512 || bmp.getHeight() != 512 || !bmp.hasAlpha()) { throw new IllegalArgumentException("所有帧必须为512x512分辨率且带透明度通道"); } } File outputFile = new File(outputPath); if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } try (ParcelFileDescriptor pfd = ParcelFileDescriptor.open(outputFile, ParcelFileDescriptor.MODE_CREATE | ParcelFileDescriptor.MODE_WRITE_ONLY)) { AnimatedWebpEncoder.Params params = new AnimatedWebpEncoder.Params(); params.setDuration(frameDurationMs * bitmaps.size()); params.setRepeatCount(Animation.RESTART); // 无限循环 params.setQuality(80); // 可调整平衡画质和体积 try (AnimatedWebpEncoder encoder = AnimatedWebpEncoder.create(pfd, params)) { for (int i = 0; i < bitmaps.size(); i++) { encoder.addFrame(bitmaps.get(i), i * frameDurationMs); // 可在此处插入进度回调逻辑 } } return outputFile.exists() && outputFile.length() < 500 * 1024; // WhatsApp要求单贴纸不超过500KB } catch (IOException e) { e.printStackTrace(); return false; } }
- 若需要兼容更低Android版本,可以修改你所用的第三方WebP编码器源码,在解析FourCC块的逻辑中添加ICCP块的跳过规则:遇到ICCP标识时,直接跳过对应块的字节长度,不需要抛出异常,ICCP是颜色元数据,不影响实际帧内容的解析转换。
方案2:修正GIF转WebP逻辑
如果必须走GIF中转,生成GIF时关闭ICC颜色配置信息写入,你当前使用的AnimatedGifEncoder默认不会写入ICCP块,检查你的代码是否在生成GIF后额外添加了元数据,剔除相关逻辑后即可解决ICCP报错。
必须遵守的WhatsApp动态贴纸规则
如果编码后的WebP仍无法被识别,检查是否符合以下要求:
- 所有帧分辨率为512x512像素
- 动画总时长不超过3秒
- 文件总大小不超过500KB
- 循环模式为无限循环
- 包含透明度通道
你现有代码的核心问题是仅将GIF字节流直接改后缀保存为WebP,本质仍是GIF格式文件,不是标准WebP文件,必然会被WhatsApp识别失败,必须经过实际的编码转换,不能直接修改文件后缀。
内容的提问来源于stack exchange,提问作者Usama Saeed US
相关产品推荐
相关产品推荐

