android - Download takes just too much time -
i'm trying download file in app, download times inconsistently long. downloading in normal time, stuck 30 seconds or more until fail due time out error.
why be?
private void download(string url, string destfilename) throws ioexception{ //todo remove // file file = new file(destfilename); // if(file.exists()) // return; if(buildconfig.debug) log.d("downloadfile", "downloading url: " + url + ", dest: " + destfilename); httpget httppost = null; androidhttpclient client = androidhttpclient.newinstance("tvinciandroid"); fileoutputstream fos = new fileoutputstream(destfilename); try { httppost = new httpget(url); httpresponse res = client.execute(httppost); if (res.getstatusline().getstatuscode() != httpstatus.sc_ok) { header[] headers = res.getheaders("location"); if(headers != null && headers.length != 0) { url = headers[headers.length - 1].getvalue(); download(url, destfilename); } } httpentity responseentity = res.getentity(); if (responseentity != null && responseentity.getcontentlength() > 0) { inputstream = androidhttpclient.getungzippedcontent(responseentity); bufferedreader reader = new bufferedreader(new inputstreamreader(is,charset.forname("utf-8"))); stringbuilder bld = new stringbuilder(); string line; while ((line = reader.readline()) != null) { line += "\n"; fos.write(line.getbytes()); bld.append(line); } reader.close(); if(buildconfig.debug) log.d("file content", bld.tostring()); bld = null; } } catch(ioexception ex){ throw ex; } { client.close(); fos.close(); } }
any appreciated
try specifying buffer 8192.
//input stream read file - 8k buffer inputstream input = new bufferedinputstream(url.openstream(), 8192);
i have sample working code here can download file via url different implementation, might you.
try { url url = new url(f_url[0]); urlconnection conection = url.openconnection(); // getting file length int lengthoffile = conection.getcontentlength(); // input stream read file - 8k buffer inputstream input = new bufferedinputstream(url.openstream(), 8192); // output stream write file outputstream output = new fileoutputstream(filepath); byte data[] = new byte[1024]; long total = 0; pdialog.setmax(lengthoffile); notification_id = 1+lengthoffile; while ((count = input.read(data)) != -1) { total += count; // publishing progress.... // after onprogressupdate called //publishprogress(""+(int)((total*100)/lenghtoffile)); publishprogress(""+(int)(total)); notifbuilder.setprogress(lengthoffile, (int)(total), false) .setcontenttext("download in progress... "+total+"/"+lengthoffile); nm.notify(notification_id, notifbuilder.build()); // writing data file output.write(data, 0, count); } // flushing output output.flush(); // closing streams output.close(); input.close(); } catch (ioexception e) { e.printstacktrace(); log.e("error: ", e.getmessage()); return e.getmessage()+" download failed!"; }
i hope helps.
Comments
Post a Comment