android - Fetching image from url and storing it locally then use these image in listiew -
i want save images url on device , use images stored on device populate listview , wanted know store images on device either on phone memory or in cache
new imagedownloader().execute(downloadurl); private class imagedownloader extends asynctask { @override protected string doinbackground(string... param) { // todo auto-generated method stub return downloadbitmap(param[0]); } @override protected void onpreexecute() { log.i("async-example", "onpreexecute called"); } @override protected void onpostexecute(string result) { log.i("async-example", "onpostexecute called"); bitmap mybitmap = bitmapfactory.decodefile(result)); imageview myimage = (imageview) findviewbyid(r.id.imageviewtest); myimage.setimagebitmap(mybitmap); simplewaitdialog.dismiss(); } private string downloadbitmap(string url) { // initilize default http client object final defaulthttpclient client = new defaulthttpclient(); //forming httoget request final httpget getrequest = new httpget(url); try { httpresponse response = client.execute(getrequest); //check 200 ok success final int statuscode = response.getstatusline().getstatuscode(); if (statuscode != httpstatus.sc_ok) { log.w("imagedownloader", "error " + statuscode + " while retrieving bitmap " + url); return null; } final httpentity entity = response.getentity(); if (entity != null) { inputstream inputstream = null; try { // getting contents stream inputstream = entity.getcontent(); // decoding stream data image bitmap android understands final bitmap bitmap = bitmapfactory.decodestream(inputstream); return savebitmaptodir(bitmap); } { if (inputstream != null) { inputstream.close(); } entity.consumecontent(); } } } catch (exception e) { // provide more explicit error message ioexception getrequest.abort(); log.e("imagedownloader", "something went wrong while" + " retrieving bitmap " + url + e.tostring()); } return null; } private string savebitmaptodir(bitmap bmp) { fileoutputstream out = null; file file = null; try { simpledateformat s = new simpledateformat("ddmmyyyy_hhmmss"); string datetime = s.format(new date()); string path = environment.getexternalstoragedirectory().tostring(); outputstream fout = null; file = new file(path, "myapp_"+datetime+".jpg"); out = new fileoutputstream(file); bmp.compress(bitmap.compressformat.png, 90, out); } catch (exception e) { e.printstacktrace(); } { try { if (out != null) { out.close(); } return file.getabsolutepath(); } catch (ioexception e) { e.printstacktrace(); } } } }
Comments
Post a Comment