How to compress pictures under Android:
About 3M of the picture can be compressed to about 100K, almost no distortion. The code is as follows:
Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.OutputStream;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory; Public classBitmaputil {/*** Save as file after compressing picture * *@paramFilePath * The full path of the original picture *@paramStoreimgpath * The full path of the picture to be stored after compression *@returnBoolean *@authorDoraemon * @time June 27, 2014 PM 5:10:19*/ Public Static Booleansavecompressimg (String filePath, String storeimgpath) {Bitmap BM=Getsmallbitmap (FilePath); OutputStream Stream=NULL; Try{Stream=NewFileOutputStream (Storeimgpath); } Catch(FileNotFoundException e) {e.printstacktrace (); } returnBm.compress (Bitmap.CompressFormat.JPEG, 40, stream); } /*** Breakthrough based on path and compression return bitmap for display * *@paramIMAGESRC *@return */ Private StaticBitmap Getsmallbitmap (String filePath) {FinalBitmapfactory.options Options =Newbitmapfactory.options (); Options.injustdecodebounds=true; Bitmapfactory.decodefile (FilePath, Options); //Calculate insamplesizeOptions.insamplesize = calculateinsamplesize (Options, 480, 800); //Decode bitmap with Insamplesize setOptions.injustdecodebounds =false; returnbitmapfactory.decodefile (FilePath, Options); } /*** Calculate the zoom value of the picture * *@paramOptions *@paramReqwidth *@paramReqheight *@return */ Private Static intCalculateinsamplesize (Bitmapfactory.options Options,intReqwidth,intreqheight) { //Raw Height and width of image Final intHeight =Options.outheight; Final intwidth =Options.outwidth; intInsamplesize = 1; if(Height > Reqheight | | width >reqwidth) { //Calculate ratios of height and width to requested height and//width Final intHeightRatio = Math.Round ((float) Height/(float) reqheight); Final intWidthRatio = Math.Round ((float) Width/(float) reqwidth); //Choose the smallest ratio as insamplesize value, this would//Guarantee//a final image with both dimensions larger than or equal to the//requested height and width.Insamplesize = HeightRatio < WidthRatio?Heightratio:widthratio; } returninsamplesize; }}