关于微信分享不出去有可能的原因--图片过大

版权声明:转载请注明出处 https://blog.csdn.net/dl10210950/article/details/53125589

前几天做的源生微信分享,测试是没有问题的可以分享出去,
昨天测试发现居然有的新闻可以分享出去,有的新闻居然不行,这是个什么耿,
想半天想不出来,还好隔壁的大神来了一句,是不是分享的图片太大了,
顿时就恍然大悟,原来我从网络上请求的图片转换成Bitmap就没有管了,直接就分享出去,难怪有的可以分享,有的又不可以,
后来查了一下,图片好像要限制在32k以下才能分享,微信这个坑比也没说,下面就是我把网络图片url转换成Bitmap然后在设置缩略图的方法
把图片网址转换成Bitmap用到了Glide,不知道的可以看看我转发的博客Glide的用法

  //把图片网址转换成bitmap,微信分享要用的
        Glide.with(NewsDetsilsActivity.this).load(imgUrl).asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                //由于微信分享的图片要求在32k一下,所以要转换成缩略图
                 mBitmap = BitmapUtils.createBitmapThumbnail(resource, false);


            }
        });
  /**
     * 设置缩略图
     *
     * @param bitMap
     * @param needRecycle
     * @return
     */
    public static Bitmap createBitmapThumbnail(Bitmap bitMap, boolean needRecycle) {
        int width = bitMap.getWidth();
        int height = bitMap.getHeight();
        // 设置想要的大小
        int newWidth = 80;
        int newHeight = 80;
        // 计算缩放比例
        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);
        if (needRecycle) bitMap.recycle();
        return newBitMap;
    }

猜你喜欢

转载自blog.csdn.net/dl10210950/article/details/53125589