分享微信朋友圈没反应,也没什么报错信息,网上大家都知道的是thumbData不能超过32K,可是我已经压缩到32K了,还是调不起来。最后发现原来主图也有限制,太坑了。
WXMediaMessage (微信媒体消息内容)说明 | 微信开放文档
图片类型分享示例
WXImageObject (WXMediaMessage.IMediaObject 的派生类,用于描述一个图片对象)
字段 | 类型 | 含义 | 备注 |
---|---|---|---|
imageData | byte[] | 图片的二进制数据 | 内容大小不超过 1MB |
imagePath | String | 图片的本地路径 | 对应图片内容大小不超过 25MB |
图片类型分享demo
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.send_img);
//初始化 WXImageObject 和 WXMediaMessage 对象
WXImageObject imgObj = new WXImageObject(bmp);
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = imgObj;
//设置缩略图
Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
bmp.recycle();
msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
//构造一个Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("img");
req.message = msg;
req.scene = mTargetScene;
req.userOpenId = getOpenId();
//调用 api 接口,发送数据到微信
api.sendReq(req);
//主图尺寸过大也分享不出来 使用imageData限制1M,使用imagePath限制25M
WXImageObject imgObj = new WXImageObject(main);
//设置缩略图
msg.thumbData = UMBitmapUtil.compressBitmap(UMBitmapUtil.bitmap2Bytes(thumb, Bitmap.CompressFormat.JPEG), 32 * 1024, Bitmap.CompressFormat.JPEG);
if (desc != null) {
msg.description = desc;
}
//构造一个Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("image");
req.message = msg;
req.scene = toTimeline ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
IWXAPI api = getWXApi(context);
api.sendReq(req);
public static byte[] compressBitmap(byte[] paramArrayOfByte, int paramInt, Bitmap.CompressFormat paramCompressFormat) {
int i = 0;
if ((paramArrayOfByte != null) && (paramArrayOfByte.length >= paramInt)) {
ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
Bitmap localBitmap = BitmapFactory.decodeByteArray(paramArrayOfByte, 0, paramArrayOfByte.length);
int j = 1;
double d = 1.0D;
while ((i == 0) && (j <= 10)) {
d = Math.pow(0.8D, j);
int k = (int) (100.0D * d);
Log.d("BitmapUtils", "quality = " + k);
localBitmap.compress(paramCompressFormat, k, localByteArrayOutputStream);
Log.d("BitmapUtils", "WeiXin Thumb Size = " + localByteArrayOutputStream
.toByteArray().length / 1024 + " KB");
if ((localByteArrayOutputStream != null) && (localByteArrayOutputStream.size() < paramInt)) {
i = 1;
} else {
localByteArrayOutputStream.reset();
j++;
}
}
if (localByteArrayOutputStream != null) {
byte[] arrayOfByte = localByteArrayOutputStream.toByteArray();
if ((arrayOfByte != null) && (arrayOfByte.length <= 0)) {
Log.e("BitmapUtils", "###32KB,");
}
return arrayOfByte;
}
}
return paramArrayOfByte;
}