Bitmap too larget to be uploaded into a texture的解决方法

Bitmap too larget to be uploaded into a texture的解决方法

问题描述

使用canvas.drawBitmap()系列方法时,抛出错误Bitmap too larget to be uploaded into a texture。

问题原因

错误日志的内容是

W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (1204x4533, max=4096x4096)

所以,可以得知问题的原因是设置src的图片宽高大于了最大接受的值,所以抛出错误。
本来想换着想法实现,经过测试发现设置background,src都会有这样的问题出现。

解决方法

使用BitmapFactory.decodeStream()系列方法将图片的宽高进行压缩。

    if (bitmap != null) {
        int maxWidth = canvas.getMaximumBitmapWidth();
        int maxHeight =  canvas.getMaximumBitmapHeight();
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        if (width > maxWidth || height > maxHeight) {
            int inSampleSize;
            int heightRatio = Math.round((float) height / (float) maxHeight);
            int widthRatio = Math.round((float) width / (float) maxWidth);
            inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
            if (inSampleSize == 1) {
                inSampleSize = 2;
            }
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = inSampleSize;
            options.inJustDecodeBounds = false;
            bitmap = BitmapFactory.decodeStream(isBm, null, options);
        }
    }

将上面这段代码添加到合适的位置,问题就解决了。

参考文章

https://blog.csdn.net/vickywinner/article/details/53164702

转载于:https://www.cnblogs.com/zhangmiao14/p/10065369.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值