WXWebpageObject webpage = new WXWebpageObject();
webpage.webpageUrl = url;
WXMediaMessage msg = new WXMediaMessage(webpage);
msg.title = title;
msg.description = content;
Resources res = context.getResources();
Bitmap bmp = null;
if (imageUrl.isEmpty()){
bmp= BitmapFactory.decodeResource(res, R.mipmap.appicon);
Bitmap thumbBitmap = Bitmap.createScaledBitmap(bmp, 150, 150, true);
msg.thumbData = WxUtil.bmpToByteArray(thumbBitmap,true);
}else {
new Thread(){
@Override
public void run() {
super.run();
Bitmap bitmap = netToLoacalBitmap(imageUrl);
int WX_THUMB_SIZE = 120;
Bitmap thumbBitmap = Bitmap.createScaledBitmap(bitmap, WX_THUMB_SIZE, WX_THUMB_SIZE, true);
msg.thumbData = WxUtil.bmpToByteArray(thumbBitmap,true);
}
}.start();
}
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.message = msg;
req.scene = type;
req.transaction = tag;
if(CommonAppContext.api==null){
CommonAppContext.api = WXAPIFactory.createWXAPI(context, Constants.WX_APP_ID, true);
CommonAppContext.api.registerApp(Constants.WX_APP_ID);
}
public static Bitmap netToLoacalBitmap(String imgUrl){
Bitmap bitmap = null;
InputStream in=null;
BufferedOutputStream out = null;
try{
in = new BufferedInputStream(new URL(imgUrl).openStream(),1024);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream,1024);
copy(in,out);
out.flush();
byte[] data = dataStream.toByteArray();
YuvImage yuvimage=new YuvImage(data, ImageFormat.NV21, 150,150, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yuvimage.compressToJpeg(new Rect(0, 0,20, 20), 80, baos);
byte[] jdata = baos.toByteArray();
bitmap = BitmapFactory.decodeByteArray(jdata, 0, jdata.length);
Log.e("--------bitmap--",bitmap+"");
data = null;
return bitmap;
}catch (IOException e){
e.printStackTrace();
return null;
}
}
提供一下复现链接