一杂谈
微信分享图片的迷之 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
来源: https://juejin.im/post/5aa25779f265da23a228b502