In Android often use to bitmap used to display pictures, if the picture is too large, prone to "OutOfMemory" exception, so the picture is compressed display.
You typically use several methods of the Bitmapfactory class (Decodebytearray (), DecodeFile (), Decoderesource (), and so on) to create a bitmap, before generating bitmap, You can use Bitmapfactory.options to set properties to ensure that outofmemory exceptions are not present. You can get a smaller multiplier by showing the length and width of the image first:
Private int calculateinsamplesize (bitmapfactory.options options, int reqwidth, int reqheight) { // raw height and width of image final int height = options.outheight; final int width = options.outwidth; int insamplesize = 1; if (height > reqheight | | width > reqwidth) { if (width > height) { insamplesize&nbsP;= math.round (float) height / (float) reqheight); } else { insamplesize = math.round (float) width / (float) reqwidth); } } return Insamplesize;}
PS: Official document said, the image compression, using a multiple of 2 compression efficiency will be high, is 2,4,8 ... This, I use here is closer to the need for compression multiples, official documents see here .
There are two ways to compress a picture, one is based on the desired image length and width, and one is based on the desired image size (that is, how many k).
Let's look at the first type:
public bitmap getthumbimagebywh (boolean isRound,String imgpath, int imagewidth, int imageheight) {try {file picture = new file (Imgpath); Bitmapfactory.options bitmapfactoryoptions = new bitmapfactory.options ();// set height and width of imagebitmapfactoryoptions.injustdecodebounds = true; Bitmap bmap = bitmapfactory.decodefile (Picture.getabsolutepath (), bitmapFactoryOptions); int insamplesize = calculateinsamplesize (Bitmapfactoryoptions,imagewidth,imageheight); bitmapfactoryoptions.insamplesize = insamplesize;bitmapfactoryoptions.injustdecodebounds = false;bmap = bitmapfactory.decodefile (Picture.getabsolutepath (), bitmapFactoryOptions); return bmap;} catch (exception e) {e.printstacktrace (); return null;}}
PS: If you use a Bitmapfactory.options object, set the Injustdecodebounds property of the object to true,insamplesize set to false after the setting is complete. The back is used to flip the picture.
The second way:
Public bitmap getthumbimagebysize (string imgpath, int size, boolean adjustorientation) {file file=new file (Imgpath); Fileinputstream fis = null;int filesize=0;try{fis = new fileinputstream ( file); filesize = fis.available (); LOG.V ("File length", "+filesize"); Fis.close ();} catch (Exception ex) {log.v ("Read file error", "" +ex.getmessage ());} if (filesize/1024<size) {return bitmapfactory.decodefile (Imgpath);} revision:bitmapfactory.options options = new bitmapfactory.options ();// Set it false to not build the bitmap,just record its width and heightoptions.injustdecodebounds = true;// get the options object by the pathbitmapfactory.decodefile (imgpath, options);int height = Options.outheight;int width =&nbSp;options.outwidth; bitmap smallbitmap = null;double multiple = (float) (width*height*4)/(float) (size *1024);int insamplesize = (int) Math.ceil (Math.sqrt (((float) filesize/1024.0)/(float) size); O ptions.insamplesize=insamplesize;options.injustdecodebounds = false;smallbitmap = Bitmapfactory.decodefile (imgpath, options); return smallbitmap; }}
Efficient Use of bitmap