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);

猜你喜欢

转载自www.cnblogs.com/niupi/p/11507002.html
今日推荐