Save itself as a file using bitmap, Bitmapfactory parse the picture from file and prevent oom

Source: Internet
Author: User


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; }
	}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.