安卓分享图片给微信好友偶尔失败
public static void weChat(Bitmap bmp ){
//初始化 WXImageObject 和 WXMediaMessage 对象
WXImageObject imgObj = new WXImageObject(bmp);
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = imgObj;
//设置缩略图
Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 100, 100, 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 = SendMessageToWX.Req.WXSceneSession;
// req.userOpenId = getOpenId();
//调用api接口,发送数据到微信
Application.getInstance().getApi().sendReq(req);
}
public IWXAPI getApi(){
return api;
}
public static Application getInstance(){
return sapplication;
}
private static final String APP_ID = "wx62c1f74a009c1111";
// IWXAPI 是第三方app和微信通信的openApi接口
private IWXAPI api;
private void regToWx() {
// 通过WXAPIFactory工厂,获取IWXAPI的实例
api = WXAPIFactory.createWXAPI(this, APP_ID, true);
// 将应用的appId注册到微信
api.registerApp(APP_ID);
//建议动态监听微信启动广播进行注册到微信
registerReceiver(new BroadcastReceiver() {
[@Override](/user/Override)
public void onReceive(Context context, Intent intent) {
// 将该app注册到微信
api.registerApp(Constants.APP_ID);
}
}, new IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP));
}
分享图片的基本都可以,这个提示是偶尔会出现,出现后过一会儿就又可以分享了