Model Millet 3w,rom4.4.4 nonsense not to say that in the load album picture times had such a mistake
09-16 12:26:53.270:w/openglrenderer (21987): Bitmap too large to BES uploaded into a texture (4208x2368, max=4096x4096)
Cut Bai, online find an algorithm, very good, posted here, recorded
In the process of doing photo album application, need to get a compressed thumbnail but, at the same time I also want to get the bitmap can be square, to adapt to the square imageview, the traditional setting insamplesize compression ratio of the way just compressed the entire picture, If a picture has a large gap between the length and width, then the display will have a stretch phenomenon, so the correct practice is after compression, the bitmap are cropped.
The code is as follows:
Captures the square portion of the middle of a given picture when it is scaled to maintain a width-to-height ratio
1 /**2 3 * @paramBitmap Original4 * @paramEdgelength want to get the square part of the side length5 * @returnzooms the bitmap after the median portion is captured. 6 */7 Public StaticBitmap Centersquarescalebitmap (Bitmap Bitmap,intedgelength)8 {9 if(NULL= = Bitmap | | Edgelength <= 0)Ten { One return NULL; A } - -Bitmap result =bitmap; the intwidthorg =bitmap.getwidth (); - intheightorg =bitmap.getheight (); - - if(widthorg > Edgelength && heightorg >edgelength) + { - //compress to a minimum length of edgelength bitmap + intLongeredge = (int) (Edgelength * Math.max (widthorg, heightorg)/math.min (widthorg, heightorg)); A intScaledwidth = widthorg > heightorg?longeredge:edgelength; at intScaledheight = widthorg > heightorg?Edgelength:longeredge; - Bitmap Scaledbitmap; - - Try{ -Scaledbitmap = Bitmap.createscaledbitmap (Bitmap, Scaledwidth, Scaledheight,true); - } in Catch(Exception e) { - return NULL; to } + - //intercept the square part in the middle. the intXtopleft = (scaledwidth-edgelength)/2; * intYtopleft = (scaledheight-edgelength)/2; $ Panax Notoginseng Try{ -result =Bitmap.createbitmap (Scaledbitmap, Xtopleft, Ytopleft, Edgelength, edgelength); the scaledbitmap.recycle (); + } A Catch(Exception e) { the return NULL; + } - } $ $ returnresult; -}
Note that the bitmap parameter must be obtained from the original image, if it has been bitmapfactory insamplesize compressed, may not be to the square.
The original page is here
Http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0917/1686.html