Bitmap too large to be uploaded into a texture 解决方案

本文介绍了解决Bitmaptoolargetobeuploadedintoatexture错误的方法,通过对图片进行压缩,确保图片能在不同设备上正常显示,避免关闭硬件加速导致的性能下降。

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

今天用ImageView加载图片时,图片并不能显示,logcat中打印出了 Bitmap too large to be uploaded into a texture (4160x3120, max=4096x4096)的错误提示。
在网上搜索了一番,因为当开启硬件加速的时候,GPU对于openglRender 有一个限制,这个不同的手机会有不同的限制:

这个限制值可以通过canvas.getMaximumBitmapHeight()和canvas.getMaximumBitmapWidth()来获得。

网上一个简单粗暴的方法是关闭硬件加速:

<application
 android:hardwareAccelerated="false" >

这样的确解决了图片,但你会在app运行的时候,发现app变得十分卡顿。

果断抛弃了这个方法,百度了好久也没找到个像样的解决方案,大多都是东抄西抄,放弃了百度。


认真看看提示,是Bitmap too large 。我突然想起很早很早之前看过郭神的一篇关于加载大图OOM的问题的一篇博客。

我们只要通过BitmapFactory.Options对象对图片进行压缩就好了。废话不多说,我直接在ImageUtil类中写下了图片压缩的方法。


public static int calculateInSampleSize(BitmapFactory.Options options,
                                            int reqWidth, int reqHeight) {
        // 源图片的高度和宽度
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            // 计算出实际宽高和目标宽高的比率
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            // 选择宽和高中最小的比率作为inSampleSize的值,这样可以保证最终图片的宽和高
            // 一定都会大于等于目标的宽和高。
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                         int reqWidth, int reqHeight) {
        // 第一次解析将inJustDecodeBounds设置为true,来获取图片大小
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);
        // 调用上面定义的方法计算inSampleSize值
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        // 使用获取到的inSampleSize值再次解析图片
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

    public static Bitmap decodeSampledBitmapFromFilePath(String imagePath,
                                                         int reqWidth, int reqHeight) {
        // 第一次解析将inJustDecodeBounds设置为true,来获取图片大小
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(imagePath, options);
        // 调用上面定义的方法计算inSampleSize值
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        // 使用获取到的inSampleSize值再次解析图片
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(imagePath,options);
    }

 然后在需要的地方调用就好了,上面的代码也是直接参考郭神的博客中的代码的,注释已经很清楚了。

最后在需要的地方调用我们的方法:

imageView.setImageBitmap(ImageUtil.decodeSampledBitmapFromFilePath(imagePath,100,100));

通过图片压缩,我们的图片能够被正常的显示了。


希望通过这篇文章,能够帮助遇到Bitmap too large to be uploaded into a texture深陷百度不能自拔的小伙伴能够快速解决。


资料参考:

郭霖  :Android高效加载大图、多图解决方案,有效避免程序OOM   

http://blog.youkuaiyun.com/sinyu890807/article/details/9316683

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值