0%

微信分享图片按质量压缩的解决方案

一、杂谈

微信分享图片的迷之bug,前一阵子接盘一个Hybrid App,分享要用原生的分享,搞了一阵子把微信分享搞上,测试又反馈了一个谜一样的Bug,我这边看Log打印了checkArgs fail, thumbData is invalid,google一番都说是图片不能超过32kb,在分享里面看到如下判断this.thumbData.length > '耀',一时间没有反应过来。。。可是怎么压缩好哇,我参照官方Demo和AndroidUtilCode的按质量压缩方法,找出了下面的解决方案,供大家参考。

二、将网络图片加载到内存

保存到本地在操作真心感觉麻烦

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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);
if (data.length > '耀') {

// 转换为bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

// 获取压缩后的byte数组
data = compressByQuality(bitmap, '耀', true);
}
return data;
}

三、将io流转为byte数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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;
}

四、按质量压缩

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 按质量压缩图片
*
* @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;
}

五、微信分享

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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