Android 关于微信分享图片过大失败的解决方案

参考:https://blog.csdn.net/dl10210950/article/details/53125589

https://blog.csdn.net/yaya_soft/article/details/11077155

这是因为微信对缩略图做了限制,最大不超过32K

所以可以先通过图片加载工具获取bitmap,然后进行压缩,压缩方法如下:

   //压缩图片
    public Bitmap createBitmapThumbnail(Bitmap bitMap) {
        int width = bitMap.getWidth();
        int height = bitMap.getHeight();
        // 设置想要的大小
        int newWidth = 99;
        int newHeight = 99;
        // 计算缩放比例
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片
        Bitmap newBitMap = Bitmap.createBitmap(bitMap, 0, 0, width, height, matrix, true);
        return newBitMap;
    }

然后封装成UMusic对象进行音频分享:

 final UMusic music = new UMusic(detail.path);//音频媒体对象
  UMImage umImage = new UMImage(mActivity, bitmap);//将缩略图封装成UMImage对象,在设置给音频媒体对象
                        music.setTitle(detail.name);
                        music.setThumb(umImage);
    new ShareAction(mActivity).setPlatform(media).setCallback(umShareListener)//设置回调
                .withTitle(detail.name)//标题
                .withText(getString(R.string.share_text_pre) + detail.name + getString(R.string.share_text_next))//描述信息
                .withMedia(uMusic)//媒体对象
                .withTargetUrl(url)//目标地址,就是点击分享要跳转的页面
                .share();

分享视频:

   String url = postShareContent(mainUrl, detail);//url点击跳转页面地址
        UMVideo image = new UMVideo(url);
        image.setTitle(detail.name);
        image.setThumb(detail.coverpath);//这里如果图片过大也应该和音频一样,
        Log.LOG = false;
        new ShareAction(mActivity).setPlatform(media).setCallback(umShareListener)
                .withTitle(detail.name)
                .withText(getString(R.string.share_text_pre) + detail.name + getString(R.string.share_text_next))
                .withMedia(image)
//                .withTargetUrl(url)//地址封装在UMVideo中,所以不必调用此方法
                .share();
        LoggerUtils.d(TAG + "\n" + String.format(" 分享地址 :%s \n 分享标题 :%s \n 分享图标 :%s \n 分享音频 :%s",
                url,detail.name,detail.coverpath,detail.path));

分享图片:

        UMImage image = new UMImage(mActivity, detail.path);
        image.setTitle(detail.name);
        image.setThumb(detail.coverpath);

        String url = postShareContent(mainUrl, detail);

        Log.LOG = false;

//        Config.IsToastTip = true;

        new ShareAction(mActivity).setPlatform(media).setCallback(umShareListener)
                .withTitle(detail.name)
                .withText(getString(R.string.share_text_pre) + detail.name + getString(R.string.share_text_next))
                .withMedia(image)
                .withTargetUrl(url)
                .share();

最后接口回调类:

    /**
     * 分享回调监听
     */
    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            LoggerUtils.d("share Result");
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            if (t != null) {
                LoggerUtils.d(t.getMessage());
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {

            LoggerUtils.d("share cancel");
        }
    };


你可能感兴趣的:(Android)