您的位置:首页 > 移动开发 > 微信开发

Android 微信SDK图片分享(checkArgs fail, thumbData is invalid)

2017-11-21 14:15 2261 查看
微信官网给的Demo中。图片的分享例子他是这么描述的:

String url = "http://pic2.nipic.com/20090506/1478953_125254084_2.jpg";

try{
WXImageObject imgObj = new WXImageObject();
imgObj.imageUrl = url;

WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = imgObj;

Bitmap bmp = BitmapFactory.decodeStream(new URL(url).openStream());
Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
bmp.recycle();
msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("img");
req.message = msg;
req.scene = isTimelineCb.isChecked() ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
api.sendReq(req);

//finish();
} catch(Exception e) {
e.printStackTrace();
Toast.makeText(SendToWXActivity.this, "Error msg"+e.toString(), 1000).show();
}

break;


而在实际的使用过程中,总是报这样的一个错误,怎么也调用不到微信的分享界面。

05-06 10:21:35.276: E/MicroMsg.SDK.WXMediaMessage(19273): checkArgs fail, thumbData is invalid
好像是图片处理那边出现了问题。细看这个代码。里面有一个将bitmap对象转化成byte数据字节的对象。原先的代码是这样的如下所示:

public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 100, output);
if (needRecycle) {
bmp.recycle();
}

byte[] result = output.toByteArray();
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}

return result;
}


现将其改成如下所示的:

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();
}
}


现在就可以对图片进行分享了。 有更好的解决办法,希望可以留言 !!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐