测试发现分享到微信朋友圈调起微信很慢,图片thumb小的话会快一些,thumb大的话会很慢,既然知道原因了,那就好处理了
友盟官方给出的建议:
现实情况是用上还是有些慢,那我们就自己压缩处理,下面说一下我处理的方法:服务端返回的是图片路径,我将图片路径转化成bitmap形式进行压缩,压缩完成后在设置上UMImage umImage = new UMImage(this, bitmap);这样调起微信会快一些,下面代码:
new Thread(new Runnable() {
@Override
public void run() {
String picUrl = goodsDetailsBean.getData().getPro_Info().get(0).get商品图片().split("\\|")[0];
Bitmap bitmap ;
if(!picUrl.equals("")){
bitmap = PhotoBitmapUtils.create(picUrl);
}else{
bitmap = BitmapFactory.decodeResource(SafShopDetailActivity.this.getResources(), R.drawable.accountdefault);
}
Message msg = new Message();
msg.what = SHARE;
msg.obj = bitmap;
mHandler.sendMessage(msg);
}
}).start();
/**
*需要在工作线程中完成
**/
public static Bitmap create(String path) {
Bitmap bit = null;
try {
URL url1 = new URL(path);
InputStream is1 = url1.openStream();
bit = BitmapFactory.decodeStream(is1);
} catch (IOException e) {
e.printStackTrace();
}
return bit;
}
public static Bitmap yasuo(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 90;
while (baos.toByteArray().length / 1024 > 128) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
baos.reset(); // 重置baos即清空baos
bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中
options -= 10;// 每次都减少10
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
return bitmap;
}
最后直接调用:shareImg(PhotoBitmapUtils.yasuo((Bitmap) msg.obj));
设置
UMImage umImage = new UMImage(this, bitmap); umWeb.setThumb(umImage);即可
如有更好的方法请赐教!