Android prevents bitmap memory overflow

Source: Internet
Author: User

How do you solve this problem when your Android development process often handles network images that send memory overflows?

Ideas:

Download to Local

Store directories on your phone via Web Capture and file download

Code:

Get Network public InputStream gethttpinfo (string urstring, string fun, String parm) throws Exception {HttpURLConnection Connec tion = (httpurlconnection) new URL (urstring). OpenConnection (); Connection.setrequestmethod (fun); Connection.setconnecttimeout (11000); Connection.setdoinput (true); Connection.setdooutput (true); Connection.setrequestproperty ("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q =0.8 "), Connection.setrequestproperty (" Connection "," keep-alive "); Connection.setrequestproperty (" User-agent "," mozilla/5.0 (Windows NT 6.1) applewebkit/537.36 (khtml, like Gecko) chrome/41.0.2272.101 safari/537.36 "); Connection.setrequestproperty ("accept-encoding", "gzip, deflate, sdch"); Connection.setrequestproperty (" Accept-language "," zh-cn,zh;q=0.8 "); OutputStream outputstream = Connection.getoutputstream (); OutputStream.write ( Parm.getbytes ()); if (connection.getresponsecode () = = HTTPURLCONNECTION.HTTP_OK) {return Connection.getinputstream () ;} return null;} File download public void DowNloadfiles (String FilePath, String Filename,inputstream inputstream) throws Exception {File File = new file (FilePath); if ( !file.exists ()) {file.mkdirs ();} FileOutputStream FileOutputStream = new FileOutputStream (new File (File,filename)); byte[] Arrs = new Byte[1024];int len = 0 ; while (len = Inputstream.read (arrs))! =-1) {fileoutputstream.write (arrs, 0, Len);} Fileoutputstream.close ();}

Then read from the local file into the bitmap object

Note: The image quality that needs to be read to modify can be obtained by modifying the aspect quality Bitmap object through the following two functions:

public static int calculateinsamplesize (bitmapfactory.options options,int reqwidth) {//the width of the source picture final int width = OPTIONS.O  Utwidth;int insamplesize = 1;if (Width > Reqwidth) {//calculates the ratio of the actual width to the target width final int widthRatio = Math.Round ((float) Width/ (float) reqwidth); insamplesize = WidthRatio;} return insamplesize;} public static Bitmap Decodesampledbitmapfromresource (String pathname,int reqwidth) {// The first resolution sets Injustdecodebounds to true to get the picture size final bitmapfactory.options Options = new Bitmapfactory.options (); O Ptions.injustdecodebounds = true; Bitmapfactory.decodefile (pathName, options);//Call the method defined above to calculate the Insamplesize value options.insamplesize = Calculateinsamplesize (options, reqwidth);//Use the obtained Insamplesize value to parse the picture again options.injustdecodebounds = False;return Bitmapfactory.decodefile (pathName, options);}

At this time bitmap quality has changed!

Original address: http://sijienet.com/bbs/?leibie=showinfo&id=51

Android prevents bitmap memory overflow

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.