This article is from: http://blog.csdn.net/wulianghuan/article/details/11548373, thanks to the original author's sharing.
The goal is to read the image from the SD card and show it.
The main idea is to compress the images from the SD card through a tool class, and finally to avoid consuming too much memory through the caching mechanism.
Util.java
Packagecom.kale.socketactivity;ImportJava.io.ByteArrayOutputStream;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory; Public classUtil {/** * @paramPath *@return* @example Bitmap Bitmap = Bitmapfactory.decodebytearray (Util.decodebitmap (ImagePath), 0, Util.decodebitmap (image Path). length); Imagecache.put (ImagePath, New softreference<bitmap> (Bitmap)); Soft references are used here.*/ Public Static byte[] Decodebitmap (String path) {Bitmapfactory.options opts=Newbitmapfactory.options (); Opts.injustdecodebounds=true;//set to True, do not consume memory, only get bitmap width highbitmapfactory.decodefile (path, opts); Opts.insamplesize= Computesamplesize (opts,-1, 1024 * 800); Opts.injustdecodebounds=false;//Be sure to set it back to false, because we set it to true beforeOpts.inpurgeable =true; Opts.ininputshareable=true; Opts.indither=false; Opts.inpurgeable=true; Opts.intempstorage=New byte[16 * 1024]; FileInputStream is=NULL; Bitmap BMP=NULL; Bytearrayoutputstream BAOs=NULL; Try{ is=NewFileInputStream (path); //To read a picture from a file, you can change it in another wayBMP = Bitmapfactory.decodefiledescriptor (IS.GETFD (),NULL, opts); DoubleScale = getscaling (Opts.outwidth *Opts.outheight,1024 * 600); Bitmap BMP2=Bitmap.createscaledbitmap (BMP, (int) (Opts.outwidth *Scale ), (int) (Opts.outheight * scale),true); Bmp.recycle (); BAOs=NewBytearrayoutputstream (); Bmp2.compress (Bitmap.CompressFormat.JPEG,100, BAOs); Bmp2.recycle (); returnBaos.tobytearray (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally { Try{is.close (); Baos.close (); } Catch(IOException e) {e.printstacktrace (); } System.GC (); } returnBaos.tobytearray (); } Private Static DoubleGetscaling (intSrcintdes) { /*** 48 Target Size ÷ original size sqrt root, to obtain a wide-high percentage of*/ DoubleScale = MATH.SQRT ((Double) des/(Double) src); returnScale ; } Public Static intcomputesamplesize (bitmapfactory.options Options,intMinsidelength,intmaxnumofpixels) { intInitialSize =computeinitialsamplesize (Options, Minsidelength, maxnumofpixels); introundedsize; if(InitialSize <= 8) {roundedsize= 1; while(Roundedsize <initialsize) {Roundedsize<<= 1; } } Else{roundedsize= (initialsize + 7)/8 * 8; } returnroundedsize; } Private Static intcomputeinitialsamplesize (bitmapfactory.options Options,intMinsidelength,intmaxnumofpixels) { DoubleW =Options.outwidth; Doubleh =Options.outheight; intLowerbound = (Maxnumofpixels = =-1)? 1: (int) Math.ceil (Math. sqrt (w* H/maxnumofpixels)); intUpperbound = (Minsidelength = =-1)? 128: (int) Math.min (Math.floor (w/minsidelength), Math.floor (H/minsidelength)); if(Upperbound <lowerbound) { returnlowerbound; } if((Maxnumofpixels = =-1) && (minsidelength = =-1)) { return1; } Else if(Minsidelength = =-1) { returnlowerbound; } Else { returnUpperbound; } }}
How to use:
Bitmap Bitmap = Bitmapfactory.decodebytearray (Util.decodebitmap (ImagePath), 0, Util.decodebitmap (ImagePath). length); New Softreference<bitmap> (Bitmap));
Implementation class
PackageCom.pioneer.travel.util;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.lang.ref.SoftReference;Importjava.net.MalformedURLException;ImportJava.net.URL;ImportJava.util.HashMap;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;Importandroid.graphics.BitmapFactory.Options;Importandroid.graphics.drawable.Drawable;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.provider.MediaStore;ImportAndroid.util.Log;ImportAndroid.widget.ImageView; Public classAsyncimageloaderbypath {//SoftReference is a soft reference and is intended to be a better system for recovering variables PrivateHashmap<string, softreference<bitmap>>Imagecache; Privatecontext Context; PublicAsyncimageloaderbypath (Context context) { This. Imagecache =NewHashmap<string, softreference<bitmap>>(); This. Context =context; } PublicBitmap Loadbitmapbypath (FinalString ImagePath,FinalImageView ImageView,Finalimagecallback Imagecallback) { if(Imagecache.containskey (ImagePath)) {//getting from the cacheSoftreference<bitmap> SoftReference =Imagecache.get (ImagePath); Bitmap Bitmap=Softreference.get (); if(Bitmap! =NULL) { returnbitmap; } } FinalHandler Handler =NewHandler () { Public voidhandlemessage (Message message) {imagecallback.imageloaded ((Bitmap) message.obj, ImageView, ImagePath) ; } }; //create a new image to get an SD card NewThread () {@Override Public voidrun () {Bitmap Bitmap= Bitmapfactory.decodebytearray (Util.decodebitmap (ImagePath), 0, Util.decodebitmap (imagePath). length); Imagecache.put (ImagePath,NewSoftreference<bitmap>(bitmap)); Message Message= Handler.obtainmessage (0, bitmap); Handler.sendmessage (message); }}.start (); return NULL; } //Callback Interface Public InterfaceImagecallback { Public voidimageloaded (Bitmap imagebitmap,imageview ImageView, String ImagePath); }}
"Go" solves the problem of OutOfMemoryError, memory overflow caused by loading multiple large graphs of Android