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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM