上一篇《Android将webp格式图片转换为png格式方法》在app上线后,部分机型报出了bug,比如华为手机,会在包含静态库的页面中直接报错奔溃,经过反复调试发现网上下载的libweb.so 之前打包的时间有点早,对高版本的系统和cpu类型支持不好,所以在github上找了一个项目,配置到gradle:2.3.1,APP_ABI := all全CPU类型,相关SO压缩文件已经放到我的下载资源里了,有需要的同学可以自行下载,或者直接git:webp-android-master项目自己编译也行,直接上代码:..
WebPDecoder类定义如下:
package me.everything.webp; import android.graphics.Bitmap; import java.nio.ByteBuffer; public class WebPDecoder { private static WebPDecoder instance = null; private WebPDecoder() { System.loadLibrary("webp_evme"); } public static WebPDecoder getInstance() { if (instance == null) { synchronized (WebPDecoder.class) { if (instance == null) { instance = new WebPDecoder(); } } } return instance; } public Bitmap decodeWebP(byte[] encoded) { return decodeWebP(encoded, 0, 0); } public Bitmap decodeWebP(byte[] encoded, int w, int h) { int[] width = new int[]{w}; int[] height = new int[]{h}; byte[] decoded = decodeRGBAnative(encoded, encoded.length, width, height); if (decoded.length == 0) return null; int[] pixels = new int[decoded.length / 4]; ByteBuffer.wrap(decoded).asIntBuffer().get(pixels); return Bitmap.createBitmap(pixels, width[0], height[0], Bitmap.Config.ARGB_8888); } public static native byte[] decodeRGBAnative(byte[] encoded, long encodedLength, int[] width, int[] height); }
使用方法:
Bitmap bp = WebPDecoder.getInstance().decodeWebP(loadFileAsByteArray(file.getAbsolutePath()));
就一句话,loadFileAsByteArray方法定在上一篇博客里,现在华为、中兴、vivo、小米手机调试无问题,测试组测试后发现问题,为再补充,先写到这里。