微信图片分享遇到 checkArgs fail, thumbData is invalid

针对微信图片上传限制问题,提供了一种改进的bmpToByteArray方法,通过调整压缩比例确保图片文件大小不超过32KB。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

该问题主要是微信图片限制32K以内的原因,可将bmpToByteArray方法进行进行改写。

原方法是:

复制代码
    /**
     * 得到Bitmap的byte
     * @author netcorner
     * @param bmp
     * @return
     */
    private  byte[] bmpToByteArray(Bitmap bmp) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, output);

        byte[] result = output.toByteArray();
        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
复制代码

 

改进的方法

复制代码
    /**
     * 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();
    }
复制代码

 

posted @  2017-05-20 17:29  netcorner 阅读( 379) 评论( 0)  编辑  收藏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值