download - Android Inputstream.read problem on Gingerbread (while downloading) -
i didn't find question here.
yesterday got gingerbread 2.3.4 on nexus one. when opened application (basically loads xml feed listview) again, got stuck while downloading.
it seems inputstream stream; -> stream.read(buffer); doesn't return -1 more, when it's finished.
the code ist same here download progress
here's code:
public inputstream getinputstreamfromurl(string urlstring, downloadprogresscallback callback) throws ioexception, illegalargumentexception { inputstream in = null; conn = (httpurlconnection) new url(urlstring).openconnection(); filesize = conn.getcontentlength(); out = new bytearrayoutputstream((int) filesize); conn.connect(); stream = conn.getinputstream(); // loop step 1kb while (status == downloading) { byte buffer[]; if (filesize - downloaded > max_buffer_size) { buffer = new byte[max_buffer_size]; } else { buffer = new byte[(int) (filesize - downloaded)]; } int read = stream.read(buffer); if (read == -1) { break; } // writing buffer out.write(buffer, 0, read); downloaded += read; // update progress bar callback.progressupdate((int) ((downloaded / filesize) * 100)); }// end of while if (status == downloading) { status = complete; } in= (inputstream) new bytearrayinputstream(out.tobytearray()); // end of class downloadimagetask() return in; }
the problem when download finishes, stream.read(buffer) returns 0 instead of -1. when change
if (read == -1) { break; }
to 0 or
if (filesize == downloaded) { break; }
i parseexceptions (expatparser) on mainactivity. on 2.2 runs perfect.
i cleared app cache , tried few other things already, i'm stuck now.
i hope can me. :)
update:
that's awesome, you're man, guillaume. :) thank much, saved evening! :)
your code needs here:
public inputstream getstreamfromurl(string urlstring, downloadprogresscallback callback){ // initialize timeouts httpparams httpparameters = new basichttpparams(); httpconnectionparams.setconnectiontimeout(httpparameters,3000); // create connection url url; try { url = new url(urlstring); urlconnection connection = url.openconnection(); httpurlconnection httpconnection = (httpurlconnection) connection; // connection accepted if(httpconnection.getresponsecode() == httpurlconnection.http_ok) { int size = connection.getcontentlength(); int index = 0; int current = 0; inputstream input = connection.getinputstream(); bufferedinputstream buffer = new bufferedinputstream(input); byte[] bbuffer = new byte[1024]; out = new bytearrayoutputstream((int) size); while((current = buffer.read(bbuffer)) != -1) { out.write(bbuffer, 0, current); index += current; callback.progressupdate((index/size)*100); } out.close(); } } catch (malformedurlexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return (inputstream) new bytearrayinputstream(out.tobytearray()); }
this code work on 2.3.4 nexus 1 :
try { // initialize timeouts httpparams httpparameters = new basichttpparams(); httpconnectionparams.setconnectiontimeout(httpparameters, 3000); // create connection url url = new url(todownload); urlconnection connection = url.openconnection(); httpurlconnection httpconnection = (httpurlconnection) connection; // connection accepted if (httpconnection.getresponsecode() == httpurlconnection.http_ok) { try { file = new file(destination); // delete file if exists file.delete(); } catch (exception e) { // nothing } int size = connection.getcontentlength(); int index = 0; int current = 0; try { file = new file(destination); file.delete(); fileoutputstream output = new fileoutputstream(file); inputstream input = connection.getinputstream(); bufferedinputstream buffer = new bufferedinputstream(input); byte[] bbuffer = new byte[10240]; while ((current = buffer.read(bbuffer)) != -1) { if (iscancelled()) { file.delete(); break; } try { output.write(bbuffer, 0, current); } catch (ioexception e) { e.printstacktrace(); } index += current; publishprogress(index / (size / 100)); } output.close(); } catch (securityexception se) { se.printstacktrace(); return 1; } catch (filenotfoundexception e) { e.printstacktrace(); return 1; } catch (exception e) { e.printstacktrace(); return 2; } return 0; } // connection refused return 2; } catch (ioexception e) { return 2; }
Comments
Post a Comment