一、雜談
微信分享圖片的迷之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);
if (data.length > '耀') {
// 轉換為bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
// 獲取壓縮後的byte陣列
data = compressByQuality(bitmap, '耀', true);
}
return data;
}
複製程式碼
三、將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