1. Use bitmap to save itself as a file
public boolean savebitmapasfile (String name, Bitmap Bitmap) {
file SaveFile = new File (cachedirectory, name);
Boolean saved = false;
FileOutputStream OS = null;
try {
log.d ("Filecache", "Saving File to Cache" + Savefile.getpath ());
OS = new FileOutputStream (saveFile);
Bitmap.compress (compressformat.png, MB, OS);
Os.flush ();
Os.close ();
Saved = true;
} catch (FileNotFoundException e) {
e.printstacktrace ();
} catch (IOException e) {
e.printstacktrace (); c15/>}
return saved;
}
2. Bitmapfactory parse the picture from file and prevent Oom
/** get the closest proportion to the desired ratio */static int calculateinsamplesize (bitmapfactory.options bitmapoptions, int reqwidth, int reqheight)
{Final int height = bitmapoptions.outheight;
Final int width = bitmapoptions.outwidth;
int samplesize = 1; if (Height > Reqheight | | width > reqwidth) {final int heightratio = Math.Round (float) height/(float) Reqheig
HT);
Final int widthRatio = Math.Round ((float) width/(float) reqwidth); SampleSize = HeightRatio < WidthRatio?
Heightratio:widthratio;
} return samplesize; } public static Bitmap Decodeimage (String filePath) {/** Decode image size */bitmapfactory.options o = new Bitmap
Factory.options ();
/** only take wide height to prevent oom */o.injustdecodebounds = true;
Bitmapfactory.decodefile (FilePath, O);
int Scale=calculateinsamplesize (o, Displaystats.maxitemwidthheight, displaystats.maxitemwidthheight);
Bitmapfactory.options options=new bitmapfactory.options (); /** Decode with Insamplesize, the less memory used in the options than the direct calculation */OPTIONS.INsamplesize=scale;
/** can be erased when the memory is low */options.inpurgeable = true;
/** deep Copy */options.ininputshareable = true;
Synchronized (ddgcontrolvar.decode_lock) {Bitmap result = Bitmapfactory.decodefile (FilePath, Options);
return result; }
}