zoukankan      html  css  js  c++  java
  • 小米3手机上加载相册图片失败的问题

    机型小米3W,ROM4.4.4废话不多说,在加载相册图片时报了这样一个错误

    09-16 12:26:53.270: W/OpenGLRenderer(21987): Bitmap too large to be uploaded into a texture (4208x2368, max=4096x4096)

    裁剪呗,网上找了个算法,挺好使,贴到这里,记录下

    在做相册应用的过程中,需要得到一个压缩过的缩略图但,同时我还希望得到的bitmap能够是正方形的,以适应正方形的imageView,传统设置inSampleSize压缩比率的方式只是压缩了整张图片,如果一个图片的长宽差距较大,则展示出来的时候会有拉伸的现象,因此正确的做法是在压缩之后,对bitmap进行裁剪。

    代码如下:

    给定图片维持宽高比缩放后,截取正中间的正方形部分

     1 /**
     2                                                                       
     3    * @param bitmap      原图
     4    * @param edgeLength  希望得到的正方形部分的边长
     5    * @return  缩放截取正中部分后的位图。
     6    */
     7   public static Bitmap centerSquareScaleBitmap(Bitmap bitmap, int edgeLength)
     8   {
     9    if(null == bitmap || edgeLength <= 0)
    10    {
    11     return  null;
    12    }
    13                                                                                  
    14    Bitmap result = bitmap;
    15    int widthOrg = bitmap.getWidth();
    16    int heightOrg = bitmap.getHeight();
    17                                                                                  
    18    if(widthOrg > edgeLength && heightOrg > edgeLength)
    19    {
    20     //压缩到一个最小长度是edgeLength的bitmap
    21     int longerEdge = (int)(edgeLength * Math.max(widthOrg, heightOrg) / Math.min(widthOrg, heightOrg));
    22     int scaledWidth = widthOrg > heightOrg ? longerEdge : edgeLength;
    23     int scaledHeight = widthOrg > heightOrg ? edgeLength : longerEdge;
    24     Bitmap scaledBitmap;
    25                                                                                   
    26           try{
    27            scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
    28           }
    29           catch(Exception e){
    30            return null;
    31           }
    32                                                                                        
    33        //从图中截取正中间的正方形部分。
    34        int xTopLeft = (scaledWidth - edgeLength) / 2;
    35        int yTopLeft = (scaledHeight - edgeLength) / 2;
    36                                                                                      
    37        try{
    38         result = Bitmap.createBitmap(scaledBitmap, xTopLeft, yTopLeft, edgeLength, edgeLength);
    39         scaledBitmap.recycle();
    40        }
    41        catch(Exception e){
    42         return null;
    43        }       
    44    }
    45                                                                                       
    46    return result;
    47   }

    需要注的是bitmap参数一定要是从原图得到的,如果是已经经过BitmapFactory inSampleSize压缩过的,可能会不是到正方形。

    原始页面在这里

    http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0917/1686.html
    
  • 相关阅读:
    pat 1034 Head of a Gang (30分)
    pta坑点
    Devc++ 编译 c++11
    Invitation Cards dijkstra法
    Cube Stacking
    畅通工程
    蚂蚁上树
    洛谷 P1439 【模板】最长公共子序列
    Recursive sequence
    A Simple Math Problem
  • 原文地址:https://www.cnblogs.com/decwang/p/4813094.html
Copyright © 2011-2022 走看看