美文网首页Android开发
微信分享图片checkArgs fail, thumbData

微信分享图片checkArgs fail, thumbData

作者: 我才是张雷 | 来源:发表于2018-10-14 11:37 被阅读0次

    产生错误的原因

    微信限制了分享图片的大小(32K)

    解决办法

      /**
         * Bitmap转换成byte[]并且进行压缩,压缩到不大于maxkb
         *
         * @param bitmap
         * @param maxKb
         * @return
         */
        public static byte[] bmpToByteArray(Bitmap bitmap, int maxKb) {
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
            int options = 100;
            while (output.toByteArray().length > maxkb && options != 10) {
                output.reset(); //清空output
                bitmap.compress(Bitmap.CompressFormat.JPEG, options, output);//这里压缩options%,把压缩后的数据存放到output中
                options -= 10;
            }
            return output.toByteArray();
        }
    

    整体代码

      /**
         * 分享到微信
         */
        private void shareToWeChat() {
            Disposable disposable = Observable.create(new ObservableOnSubscribe<Bitmap>() {
                @Override
                public void subscribe(ObservableEmitter<Bitmap> e) throws Exception {
                    Glide.with(mContext)
                            .load(mCollageDetailBean.getImg())
                            .asBitmap()
                            .centerCrop()
                            .override(200, 200)
                            .into(new SimpleTarget<Bitmap>() {
                                @Override
                                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                    e.onNext(resource);
                                }
                            });
                }
            }).subscribeOn(io.reactivex.android.schedulers.AndroidSchedulers.mainThread())
                    .map(new Function<Bitmap, byte[]>() {
                        @Override
                        public byte[] apply(Bitmap bitmap) throws Exception {
                            return bmpToByteArray(bitmap, 32);
                        }
                    })
                    .subscribeOn(io.reactivex.schedulers.Schedulers.io())
                    .observeOn(io.reactivex.android.schedulers.AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<byte[]>() {
                        @Override
                        public void accept(byte[] bytes) throws Exception {
                            showLoading();
                            WXWebpageObject webpageUrl = new WXWebpageObject();
                            webpageUrl.webpageUrl = "分享的链接";
                            WXMediaMessage msg = new WXMediaMessage(webpageUrl);
                            msg.title = "分享的标题";
                            msg.description = "分享的说明内容";
                            msg.thumbData = bytes;                
                            SendMessageToWX.Req req = new SendMessageToWX.Req();
                            req.transaction = "";
                            req.message = msg;
                            req.scene = SendMessageToWX.Req.WXSceneSession;  // WXSceneSession会话  WXSceneTimeline朋友圈
                            String weixin_appid = Order365App.getInstance().getWEIXIN_APPID();
                            IWXAPI api = WXAPIFactory.createWXAPI(mContext, weixin_appid);
                            boolean b = api.sendReq(req);
                            if (!b) {
                                showError("分享失败");
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            showLoading();
                            showError("分享失败");
                        }
                    });
    
            addDisposable(disposable);
        }
    

    这里用Glide去加载生成Bitmap是为了方便剪裁

    相关文章

      网友评论

        本文标题:微信分享图片checkArgs fail, thumbData

        本文链接:https://www.haomeiwen.com/subject/frezaftx.html