Android image downloading -
i want download image android phone , follow downloading prossess . downloading working ,but didn't found way how tho follow in log tag==> final target determine speed of download :
`at t=0,1s 40byte downloaded` @ t=10s 2000byt downloaded ...
here code ,i hope me ,and thank
public class downloadactivity extends activity { private menuitem item; private string url = "http://10.0.2.2:8080/testandroid/downloadservlet"; //http://localhost:8080/testandroid/downloadservlet/logo.png private string name = "/logo.png"; imageview imgview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_download); imgview = (imageview) findviewbyid(r.id.imgview1); string[] params = new string[] {url, name}; sendhttprequesttask task = new sendhttprequesttask(); task.execute(params); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); item = menu.getitem(0); return true; } private class sendhttprequesttask extends asynctask<string, integer, byte[]> { @override protected byte[] doinbackground(string... params) { string url = params[0]; string name = params[1]; log.i("log_tagaaaa",url ); log.i("log_tagaaaaa",name ); defaulthttpclient client = new defaulthttpclient(); httpresponse response = null; string urlstring =url+name; log.v("hub", "url"+urlstring); string __url=urlstring.replace(" ", "%20"); httpget httpget=new httpget(__url); httpget.setheader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); httpget.setheader("accept-language", "fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3"); httpget.setheader("accept-charset", "iso-8859-1,utf-8;q=0.7,*;q=0.7"); httpget.setheader("user-agent", "mozilla/5.0 (linux; u; android 3.0.1; en-us; gt-p7100 build/hri83) applewebkit/534.13 (khtml, gecko) version/4.0 safari/534.13"); try { response = client.execute(httpget); //tableau d'octets en mémoire bytearrayoutputstream baos = new bytearrayoutputstream(); inputstream in ; in = response.getentity().getcontent(); if (response.getstatusline().getstatuscode() == 200) { byte[] b = new byte[1024]; long total =0; string d; long a; //le telechargement long starttime = system.currenttimemillis(); int =0 ; while ( in.read(b) != -1) { baos.write(b); a= response.getentity().getcontentlength(); d=a+""; log.i("log_tag_taille",d ); } // bytes_so_far : number of bytes download far. //column_bytes_downloaded_so_far : public static final string string bytes_so_far = downloadmanager.column_bytes_downloaded_so_far ; log.i("log_tag_download",downloadmanager.column_bytes_downloaded_so_far ); long endtime = system.currenttimemillis(); // a= b.length; log.i("log_tag_starttime",starttime +"" ); log.i("log_tag_endtime",endtime +"" ); log.i("log_tag_difference",endtime-starttime +"" ); hashmap<string, object> returnhashmap =new hashmap<string, object>(); } else { return null ; } } catch (clientprotocolexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } // getcontentlength va me donner la taille totale du fichier téléchargé //returnhashmap.put(length_buffer, new long(response.getentity().getcontentlength())); return null ; } protected void onprogressupdate(integer...progress) { super.onprogressupdate(progress[0]); log.i("progress update: " , progress[0].tostring()); } @override protected void onpostexecute(byte[] result) { bitmap img = bitmapfactory.decodebytearray(result, 0, result.length); string d=result.length + ""; log.i("log_tagaaaaa onpostexecute",d ); imgview.setimagebitmap(img); item.setactionview(null); } } }
well after little serche found solution had change loop while shoshi said, i recommand tutorial :
and while loop changed tutrial ,according needs :`
while ((count = input.read(data)) != -1) { total += count; long endttime = system.currenttimemillis(); long passed; passedtime =endttime - starttotaltime; // passed=passedtime -previostime ; // previostime = passed ; // publishing progress.... // after onprogressupdate called publishprogress(""+(int)((total*100)/lenghtoffile)); log.i("log_lenghtoffile",lenghtoffile+"" ); log.i("log_total",total+"" ); log.i("log_ourcentage",(int)((total*100)/lenghtoffile)+"" ); log.i("log_passed_time",passedtime +"" ); // log.i("log_debit",passedtime +"" ); // writing data file // output.write(data, 0, count); } long endtotaltime = system.currenttimemillis(); log.i("log_total_passed_time",endtotaltime-starttotaltime +"" );`
i hope :)
Comments
Post a Comment