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