一、杂谈
微信分享图片的迷之bug,前一阵子接盘一个Hybrid App,分享要用原生的分享,搞了一阵子把微信分享搞上,测试又反馈了一个谜一样的Bug,我这边看Log打印了checkArgs fail, thumbData is invalid,google一番都说是图片不能超过32kb,在分享里面看到如下判断this.thumbData.length > '耀',一时间没有反应过来。。。可是怎么压缩好哇,我参照官方Demo和AndroidUtilCode的按质量压缩方法,找出了下面的解决方案,供大家参考。
二、将网络图片加载到内存保存到本地在操作真心感觉麻烦
public static byte[] getHtmlByteArray(final String url) {
URL htmlUrl = null;
InputStream inStream = null;
try {
htmlUrl = new URL(url);
URLConnection connection = htmlUrl.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
inStream = httpConnection.getInputStream();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 将io流转为byte数组
byte[] data = inputStreamToByte(inStream);
// 转换为bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
// 获取压缩后的byte数组
byte[] bytes = compressByQuality(bitmap, '耀', true);
return bytes;
}
三、将io流转为byte数组
public static byte[] inputStreamToByte(InputStream is) {
try {
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int ch;
while ((ch = is.read()) != -1) {
bytestream.write(ch);
}
byte imgdata[] = bytestream.toByteArray();
bytestream.close();
return imgdata;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
四、按质量压缩
/**
* 按质量压缩图片
*
* @param src bitmap图片
* @param maxByteSize 最大字节数
* @param recycle
* @return
*/
public static byte[] compressByQuality(final Bitmap src, final long maxByteSize, final boolean recycle) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
src.compress(CompressFormat.JPEG, 100, baos);
byte[] bytes;
if (baos.size() <= maxByteSize) {
bytes = baos.toByteArray();
} else {
baos.reset();
src.compress(CompressFormat.JPEG, 0, baos);
if (baos.size() >= maxByteSize) {
bytes = baos.toByteArray();
} else {
// find the best quality using binary search
int st = 0;
int end = 100;
int mid = 0;
while (st < end) {
mid = (st + end) / 2;
baos.reset();
src.compress(CompressFormat.JPEG, mid, baos);
int len = baos.size();
if (len == maxByteSize) {
break;
} else if (len > maxByteSize) {
end = mid - 1;
} else {
st = mid + 1;
}
}
if (end == mid - 1) {
baos.reset();
src.compress(CompressFormat.JPEG, st, baos);
}
bytes = baos.toByteArray();
}
}
if (recycle && !src.isRecycled()) src.recycle();
return bytes;
}
五、微信分享
WXWebpageObject object = new WXWebpageObject();
object.webpageUrl = url;
WXMediaMessage wxMediaMessage = new WXMediaMessage(object);
// 标题
wxMediaMessage.title = title;
// 描述
wxMediaMessage.description = des.substring(0, 20);
// 图片
wxMediaMessage.thumbData = Util.getHtmlByteArray(image);
//构造一个Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = String.valueOf(System.currentTimeMillis());
req.message = wxMediaMessage;
req.scene = SendMessageToWX.Req.WXSceneSession;
mIWXAPI.sendReq(req);
六、源码
WechatShareUtil类
https://github.com/sdwfqin/AndroidQuick
张钦,原文地址
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦