Android development, image processing is very common, often need to user selected images uploaded to the server, but now the resolution of the phone more and more good, any photo is 2M or more, if directly displayed in the ImageView, will appear oom, Uploading to such as the server also consumes a lot of traffic, the user experience must be bad!
The following own implementation of the image display and compression function, the main code is copied from the volley imagerequest, as a tool for easy image processing
Package Com.img.util;import Java.io.file;import Java.io.filenotfoundexception;import java.io.FileOutputStream; Import Java.io.outputstream;import Android.content.context;import Android.database.cursor;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.Bitmap.CompressFormat; Import Android.net.uri;import Android.os.environment;import Android.provider.mediastore.images;import android.util.log;/** * Image Compression Tool class * * @author Administrator * */public class Imagecompress {public static final String CONT ENT = "Content";p ublic static final String file = "file";/** * Picture compression parameter * * @author Administrator * */public Static class compressoptions {public static final int default_width = 400;public static final int default_height = 800;public int Maxwi DTH = default_width;public int maxheight = default_height;/** * Compressed picture saved file */public files destfile;/** * Picture compression format, default to JPG format */ Public Compressformat Imgformat = compressformat.jpeg;/** * Picture Compression ratio defaults to-*/public int quality = 30;public URI Uri;} Public Bitmap Compressfromuri (Context context,compressoptions compressoptions) {//URI points to the file path string FilePath = GetFilePath (context, Compressoptions.uri); if (null = = FilePath) {return null;} Bitmapfactory.options Options = new Bitmapfactory.options (); options.injustdecodebounds = true; Bitmap temp = bitmapfactory.decodefile (FilePath, options); int actualwidth = Options.outwidth;int ActualHeight = Options.outheight;int desiredwidth = getresizeddimension (Compressoptions.maxwidth,compressoptions.maxheight, ActualWidth, actualHeight); int desiredheight = Getresizeddimension (Compressoptions.maxheight, Compressoptions.maxwidth, ActualHeight, actualwidth); options.injustdecodebounds = False;options.insamplesize = Findbestsamplesize (ActualWidth, Actualheight,desiredwidth, desiredheight); Bitmap Bitmap = null; Bitmap Destbitmap = Bitmapfactory.decodefile (FilePath, options);/If necessary, scale down to the maximal acceptable size . if (Destbitmap.getwidth () > desiredwidth| | destbitmAp.getheight () > Desiredheight) {bitmap = Bitmap.createscaledbitmap (Destbitmap, Desiredwidth,desiredheight, true); Destbitmap.recycle ();} else {bitmap = Destbitmap;} Compress file if needif (null! = Compressoptions.destfile) {compressfile (compressoptions, bitmap);} return bitmap;} /** * Compress file from bitmap with compressoptions * * @param compressoptions * @param bitmap */private void Compressfi Le (compressoptions compressoptions, Bitmap Bitmap) {OutputStream stream = null;try {stream = new FileOutputStream (compres Soptions.destfile);} catch (FileNotFoundException e) {log.e ("imagecompress", E.getmessage ());} Bitmap.compress (Compressoptions.imgformat, compressoptions.quality,stream);} private static int findbestsamplesize (int actualwidth, int actualheight,int desiredwidth, int desiredheight) {Double WR = (double) actualwidth/desiredwidth;double hr = (double) actualheight/desiredheight;double ratio = Math.min (WR, HR); Floa T n = 1.0f;while ((n * 2) <= ratio) {n *= 2;} return (int) n;} private static int getresizeddimension (int maxprimary, int maxsecondary,int actualprimary, int actualsecondary) {//If No Dominant value at all, just return the actual.if (maxprimary = = 0 && maxsecondary = 0) {return actualprimary;} If primary is unspecified, scale primary to match secondary ' s scaling//ratio.if (maxprimary = = 0) {Double ratio = (dou BLE) maxsecondary/(double) Actualsecondary;return (int) (actualprimary * ratio);} if (maxsecondary = = 0) {return maxprimary;} Double ratio = (double) actualsecondary/(double) actualprimary;int resized = maxprimary;if (resized * ratio > Maxseco Ndary) {resized = (int) (maxsecondary/ratio);} return resized;} /** * Get the path of the file * * @param scheme * @return */private string GetFilePath (context context, Uri uri) {string filePath = null; if (Content.equalsignorecase (Uri.getscheme ())) {cursor cursor = context.getcontentresolver (). Query (Uri,new string[] { Images.Media.DATA}, NULL, NULL, NULL); if (null = = cursor) {return null;} try {if (Cursor.movetonext ()) {FilePath = cursor.getstring (Cursor.getcolumnindex (Images.Media.DATA));}} finally {cursor.close ();}} Select if (File.equalsignorecase (Uri.getscheme ())) {FilePath = Uri.getpath () from the file;} return filePath;}}How to use:
<span style= "White-space:pre" ></span>imagecompress compress = new imagecompress (); I Magecompress.compressoptions options = new Imagecompress.compressoptions (); Options.uri = imguri;options.maxwidth= Getwindowmanager (). Getdefaultdisplay (). getwidth (); Options.maxheight=getwindowmanager (). GetDefaultDisplay (). GetHeight (); Bitmap Bitmap = Compress.compressfromuri (this, options);
The default compression scale is 30%, which can be modified according to the actual situation.
Android compresses picture size to prevent oom