Android 微信分享,微信分享图片

直接贴代码吧

微信分享正常连接

WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = string;
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = "标题";
        msg.description = "内容";
        //这里替换一张自己工程里的图片资源
        Bitmap thumb = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        msg.setThumbImage(thumb);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = flag == 0 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
        wxApi.sendReq(req);

微信分享加网络图片

imageUrl 网络图片地址。
new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    WXWebpageObject webpage = new WXWebpageObject();
                    webpage.webpageUrl = string;
                    WXMediaMessage msg = new WXMediaMessage(webpage);
                    msg.title = title;
                    msg.description = des;
                    Bitmap bmp = BitmapFactory.decodeStream(new URL(imageUrl).openStream());
                    Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true);
                    bmp.recycle();
                    msg.thumbData = Bitmap2Bytes(thumbBmp);
                    SendMessageToWX.Req req = new SendMessageToWX.Req();
                    req.transaction = String.valueOf(System.currentTimeMillis());
                    req.message = msg;
                    req.scene = flag == 0 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
                    wxApi.sendReq(req);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

图片转byte数组

 // 图片转 byte[] 数组
    public byte[] Bitmap2Bytes(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

小代码是日常生活。

 

 

 

你可能感兴趣的:(Android,studio,Android,微信,android)