Android微信分享网络图片缩略图

   /**
     * wx分享
     *
     * @param type
     */
    private static void setShare(Activity activity, String type, String url, String title, String image) {
     
        mApi = WXAPIFactory.createWXAPI(activity, "官网appid");
        mApi.registerApp("官网appid");
        // 通过appId得到IWXAPI这个对象
        // 检查手机或者模拟器是否安装了微信
        if (!mApi.isWXAppInstalled()) {
     
            ToastUtil.show("您还没有安装微信");
            return;
        }

        new Thread(new Runnable() {
     
            @Override
            public void run() {
     
                try {
     
                    // 初始化一个WXWebpageObject对象
                    WXWebpageObject webpageObject = new WXWebpageObject();
                    // 填写网页的url
                    webpageObject.webpageUrl = url;

                    // 用WXWebpageObject对象初始化一个WXMediaMessage对象
                    WXMediaMessage msg = new WXMediaMessage(webpageObject);
                    // 填写网页标题、描述、位图
                    msg.title = title;
                    msg.description = title;


                    Bitmap bmp = null;
                    //image网络图片
                    bmp = BitmapFactory.decodeStream(new URL(image).openStream());

                    Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true);
                    bmp.recycle();
                    msg.thumbData = Bitmap2Bytes(thumbBmp);
                    // 构造一个Req
                    SendMessageToWX.Req req = new SendMessageToWX.Req();
                    // transaction用于唯一标识一个请求(可自定义)
                    req.transaction = "webpage";
                    // 上文的WXMediaMessage对象
                    req.message = msg;
                    //根据type设置分享情景
                    switch (type) {
     
                        case TYPE_WECHAT:
                            //分享到微信好友
                            req.scene = SendMessageToWX.Req.WXSceneSession;
                            break;
                        case TYPE_WECHAT_MOMENT:
                            //分享到微信朋友圈
                            req.scene = SendMessageToWX.Req.WXSceneTimeline;
                            break;
                    }
                    mApi.sendReq(req);
                } catch (Exception e) {
     
                    e.printStackTrace();
                }
            }
        }).start();
    }
 // 图片转 byte[] 数组
    public static byte[] Bitmap2Bytes(Bitmap bm) {
     
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

转载链接

你可能感兴趣的:(Android微信分享网络图片缩略图)