Android Network Download pictures

Source: Internet
Author: User

2 methods:

1.

Public byte[] Downloadresource (context context, String URL) throws Clientprotocolexception, IOException {isstop = false; Bytearraybuffer buffer = null; HttpGet hp = new HttpGet (URL), httpClient = new Defaulthttpclient (); String NetType = isnettype (context); if (NetType! = null & Nettype.equals ("Cmwap")) {Httphost proxy = new Httphost ("10. 0.0.172, Httpclient.getparams (). Setparameter (Connrouteparams.default_proxy,proxy);} Httpconnectionparams.setconnectiontimeout (Httpclient.getparams (), 5 * 1000); Httpconnectionparams.setsotimeout (Httpclient.getparams (), 60 * 1000); HttpResponse response = Httpclient.execute (HP), if (Response.getstatusline (). Getstatuscode () = = +) {InputStream = Response.getentity (). getcontent (); if (InputStream! = null) {int i = (int) response.getentity (). Getcontentlength (); Buffer = new Bytearraybuffer (1024x768); byte[] tmp = new Byte[1024];int len;while ((len = Inputstream.read (tmp))! =-1) &&A mp (false = = Isstop)) {buffer.append (tmp, 0, Len);}} Cancel ();} Return buffer.toByteArray ();} 
Call Method:

Protected Bitmap doinbackground (Wonderfulprograminfo ... params) {Bitmap Bitmap = null;try{string urls = constant.url + par Ams[0].getwonderfulimgurl (); Boolean isexists = Files.compare (URLs), if (isexists = = False) {//network download picture data Net net = new Net (); byte[] data = Net.downloadresource (homeactivity.this, URLs); bitmap = Bitmapfactory.decodebytearray (data, 0, Data.length); Imagescache.put (URLs, bitmap); Save the downloaded image to the cache Files.saveimage (URLs, data);} else{//local reading of image data byte[] data = files.readimage (URLs); Bitmapfactory.options opt = new bitmapfactory.options (); opt.inpreferredconfig = Bitmap.config.rgb_565;o pt.inpurgeable = True;opt.ininputshareable = true;////Get resource picture//InputStream is =//context.getresources (). Openrawresource (resId);//Return Bitmapfactory.decodestream (is,null,opt); bitmap = Bitmapfactory.decodebytearray ( Data, 0, data.length, opt); Imagescache.put (URLs, bitmap); Save the downloaded image to the cache}

Method Two:

Class Downloadtask extends Asynctask<string, Void, bitmap> {private ImageView imageview;private Integer positions; Public Downloadtask (ImageView view, int position) {ImageView = View;this.positions = position;} Protected Bitmap doinbackground (String ... params) {URL url;try {url = new URL (params[0]); InputStream is = Url.openstream () ; Bitmapfactory.options opt = new bitmapfactory.options (); opt.inpreferredconfig = Bitmap.config.rgb_565;o pt.inpurgeable = True;opt.ininputshareable = true;//Gets the resource picture bitmap bitmap = Bitmapfactory.decodestream (is, null, opt); HomeActivity.TopGalleryBitmap.put (positions, bitmap); return bitmap;} catch (OutOfMemoryError err) {err.printstacktrace ();} catch (Malformedurlexception e) {e.printstacktrace ();} catch ( IOException e) {e.printstacktrace ();} return null;} protected void OnPostExecute (Bitmap result) {Super.onpostexecute (result); Imageview.setimagebitmap (result);}}


Android Network Download pictures

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.