小米3手机上加载相册图片失败的问题

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

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

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

在作相册应用的过程当中,须要获得一个压缩过的缩略图但,同时我还但愿获得的bitmap可以是正方形的,以适应正方形的imageView,传统设置inSampleSize压缩比率的方式只是压缩了整张图片,若是一个图片的长宽差距较大,则展现出来的时候会有拉伸的现象,所以正确的作法是在压缩以后,对bitmap进行裁剪。算法

代码以下:spa

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

 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压缩过的,可能会不是到正方形。htm

原始页面在这里blog

http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0917/1686.html
相关文章
相关标签/搜索