Android 加载超长大图(长度超过4096)的解决方案和处理办法

最近工作上发现一个bug,图片加载不出来。显示黑屏,什么也没有,可是图片地址没有问题呀。

最后查看log发现有个报错

Bitmap too large to be uploaded into a texture (1445x6459, max=4096x4096)

意思就是bitmap的长图超长了,大于了4096,。

最后经过查询发现有两种解决办法。

一:把bitmap的长度压制4096

    // 利用矩阵并指定宽高
    public static Bitmap resizeImage(Bitmap bitmap, int w, int h) {
        // 原图的bitmap
        Bitmap BitmapOrg = bitmap;
        // 原图的宽高
        int width = BitmapOrg.getWidth();
        int height = BitmapOrg.getHeight();
        // 指定的新的宽高
        int newWidth = w;
        int newHeight = h;
        // 计算的缩放比例
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 用于缩放的矩阵
        Matrix matrix = new Matrix();
        // 矩阵缩放
        matrix.postScale(scaleWidth, scaleHeight);
        // 如果要旋转图片
        // matrix.postRotate(45);
        // 生成新的bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
                height, matrix, true);
        return resizedBitmap;
    }

方法二: 把图片分成两截分别在两个ImageView上显示

bitmap是你的需要截取的图片bitmap

 Bitmap topBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), (int) (bitmap.getHeight() / 2.0f));
            Bitmap bottomBitmap = Bitmap.createBitmap(bitmap, 0, (int) (bitmap.getHeight() / 2.0f), bitmap.getWidth(),
                    bitmap.getHeight() - (int) (bitmap.getHeight() / 2.0f));
            mImageView.setImageBitmap(topBitmap);
            mImageView2.setImageBitmap(bottomBitmap);

 

转载于:https://www.cnblogs.com/niupi/p/11507002.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值