一、问题现象
在做微信分享功能时,分享网页加缩略图时,点击分享一直没反应。经过查看日志
checkArgs fail, thumbData is invalid 出现这样的一个错误提示。
二、原因分析
经过百度一番:原来微信对缩略图的大小进行了限制。超过32K 就会报参数错误!
final boolean checkArgs() {
if(this.getType() != 8 || this.thumbData != null && this.thumbData.length != 0) {
if(this.getType() == 36 && (this.thumbData == null || this.thumbData.length > 131072)) {
Log.e("MicroMsg.SDK.WXMediaMessage", "checkArgs fail, thumbData should not be null or exceed 128kb");
return false;
} else if(this.getType() != 36 && this.thumbData != null && this.thumbData.length > '耀') {
Log.e("MicroMsg.SDK.WXMediaMessage", "checkArgs fail, thumbData is invalid");
return false;
} else if(this.title != null && this.title.length() > 512) {
Log.e("MicroMsg.SDK.WXMediaMessage", "checkArgs fail, title is invalid");
return false;
} else if(this.description != null && this.description.length() > 1024) {
Log.e("MicroMsg.SDK.WXMediaMessage", "checkArgs fail, description is invalid");
return false;
} else if(this.mediaObject == null) {
Log.e("MicroMsg.SDK.WXMediaMessage", "checkArgs fail, mediaObject is null");
return false;
} else if(this.mediaTagName != null && this.mediaTagName.length() > 64) {
Log.e("MicroMsg.SDK.WXMediaMessage", "checkArgs fail, mediaTagName is too long");
return false;
} else if(this.messageAction != null && this.messageAction.length() > 2048) {
Log.e("MicroMsg.SDK.WXMediaMessage", "checkArgs fail, messageAction is too long");
return false;
} else if(this.messageExt != null && this.messageExt.length() > 2048) {
Log.e("MicroMsg.SDK.WXMediaMessage", "checkArgs fail, messageExt is too long");
return false;
} else {
return this.mediaObject.checkArgs();
}
} else {
Log.e("MicroMsg.SDK.WXMediaMessage", "checkArgs fail, thumbData should not be null when send emoji");
return false;
}
}
三、解决方法
修改微信提供的方法:
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
if (needRecycle) {
bmp.recycle();
}
byte[] result = output.toByteArray();
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
使用这个方法:注意要是还是不行 ----》 将100 调小!!! 至10 试试
localBitmap.compress(Bitmap.CompressFormat.JPEG, 100,
localByteArrayOutputStream);
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
int i;
int j;
if (bmp.getHeight() > bmp.getWidth()) {
i = bmp.getWidth();
j = bmp.getWidth();
} else {
i = bmp.getHeight();
j = bmp.getHeight();
}
Bitmap localBitmap = Bitmap.createBitmap(i, j, Bitmap.Config.RGB_565);
Canvas localCanvas = new Canvas(localBitmap);
while ( true) {
localCanvas.drawBitmap(bmp, new Rect(0, 0, i, j), new Rect(0, 0,i, j), null);
if (needRecycle)
bmp.recycle();
ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
localBitmap.compress(Bitmap.CompressFormat.JPEG, 100,
localByteArrayOutputStream);
localBitmap.recycle();
byte[] arrayOfByte = localByteArrayOutputStream.toByteArray();
try {
localByteArrayOutputStream.close();
return arrayOfByte;
} catch (Exception e) {
// F.out(e);
}
i = bmp.getHeight();
j = bmp.getHeight();
}
}