Android 微信分享图片问题集锦

之前在做微信分享时碰到如下问题。

02-10 17:35:48.193 12867-12867/? W/System.err: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tencent.mm/com.tencent.mm.ui.transmit.SendAppMessageWrapperUI}: java.lang.NullPointerException: Attempt to invoke interface method 'int com.tencent.mm.sdk.modelmsg.WXMediaMessage$b.type()' on a null object reference
02-10 17:35:48.193 12867-12867/? W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2651)
02-10 17:35:48.193 12867-12867/? W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2715)
02-10 17:35:48.193 12867-12867/? W/System.err:     at android.app.ActivityThread.access$800(ActivityThread.java:178)
02-10 17:35:48.193 12867-12867/? W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1579)
02-10 17:35:48.193 12867-12867/? W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:111)
02-10 17:35:48.193 12867-12867/? W/System.err:     at android.os.Looper.loop(Looper.java:199)
02-10 17:35:48.193 12867-12867/? W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5783)
02-10 17:35:48.193 12867-12867/? W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
02-10 17:35:48.193 12867-12867/? W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
02-10 17:35:48.193 12867-12867/? W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
02-10 17:35:48.193 12867-12867/? W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:777)
02-10 17:35:48.193 12867-12867/? W/System.err: Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int com.tencent.mm.sdk.modelmsg.WXMediaMessage$b.type()' on a null object reference
02-10 17:35:48.193 12867-12867/? W/System.err:     at com.tencent.mm.ui.transmit.SendAppMessageWrapperUI.onCreate(SourceFile:148)
02-10 17:35:48.193 12867-12867/? W/System.err:     at android.app.Activity.performCreate(Activity.java:6060)
02-10 17:35:48.193 12867-12867/? W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125)
02-10 17:35:48.193 12867-12867/? W/System.err:     at com.tencent.mm.performance.a.a$b.callActivityOnCreate(SourceFile:59)
02-10 17:35:48.193 12867-12867/? W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2604)
02-10 17:35:48.193 12867-12867/? W/System.err:  ... 10 more
我的代码如下
//初始化WxImageObject和WxMediaMessage对象
WXImageObject imgObj = new WXImageObject();
imgObj.setImagePath(mFile.getAbsolutePath());

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

Bitmap bmp = BitmapFactory.decodeFile(mFile.getAbsolutePath());
Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 120, 120, true);
wxMsg.thumbData = WxUtils.bmpToByteArray(thumbBmp, true);
bmp.recycle();
//设置请求
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("img");
req.message = wxMsg;
req.scene = SendMessageToWX.Req.WXSceneSession;
MyApp.wxApi.sendReq(req);
WXEntryActivity
    @Override
    public void onResp(BaseResp baseResp) {
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                if (baseResp.getType() == ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX) { //分享
                    L.i("微信分享成功");
                } else if (baseResp.getType() == ConstantsAPI.COMMAND_SENDAUTH) {
                    //拿到了微信返回的code,立马再去请求access_token
                    String code = ((SendAuth.Resp) baseResp).code; //授权code
                    //发送请求获取access_token
                    getAccessToken(code);
                    L.i("微信登陆认证成功");
                }
                WXEntryActivity.this.finish();
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                L.i("微信请求取消");
                //发送广播告知PersonFrament,取消了登陆
                Intent intent = new Intent(IConstants.loginReceiver);
                intent.putExtra("cancel", true);
                sendBroadcast(intent);
                WXEntryActivity.this.finish();
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                L.i("微信请求失败");
                WXEntryActivity.this.finish();
                break;
            default:
                WXEntryActivity.this.finish();
                break;
        }
    }

虽然做了无数遍微信分享,但是却碰到了如上大坑。最后在微信官方文档中查看Android常见问题
发现了问题

Q:调用wxapi.sendReq接口,返回true,但微信客户端并未启动,请检查以下几项:
A:
1)微信是否安装
2)调用时的Apk包名和签名是否与开放平台填写的一致,签名请使用该工具:点击下载,常发生在安装了debug版本又安装release版本情况,确定包名签名后卸载微信重装或者清除微信数据再做测试
3)检查发送时的缩略图大小是否超过32k
4)能够调起微信到选择好友列表,但是点击发送后无响应,请检查proguard配置是否对微信SDK代码进行了混淆,建议不要对SDK对混淆,参考以下proguard配置:
-keep class com.tencent.mm.sdk.** {
*;
}

排除123,发现我在打包的时候没有加上混淆代码,所以出现以上问题。加上最后成功如愿解决了分享问题。

你可能感兴趣的:(Android 微信分享图片问题集锦)