java - Unable to download complete file in android app -
following code,
from activity class
intent intent = new intent(this, downloadservice.class); // create new messenger communication messenger messenger = new messenger(handler); intent.putextra("messenger", messenger); intent.setdata(uri.parse("http://www.abc.ezy.asia/e-mobapps/op.apk")); intent.putextra("urlpath", "http://www.abc.ezy.asia/e-mobapps/op.apk"); startservice(intent);
i have overrided service class method onhandle event
// downloadservice class @override protected void onhandleintent(intent intent) { uri data = intent.getdata(); string urlpath = intent.getstringextra("urlpath"); string filename = data.getlastpathsegment(); file output = new file(environment.getexternalstoragedirectory(),filename); if (output.exists()) { output.delete(); } inputstream stream = null; fileoutputstream fos = null; try { url url = new url(urlpath); stream = url.openconnection().getinputstream(); fos = new fileoutputstream(output.getpath()); byte datab[] = new byte[1024]; inputstreamreader reader = new inputstreamreader(stream); int next = -1; while ((next = reader.read()) != -1) { fos.write(next); } fos.flush(); result = activity.result_ok; } catch (exception e) { e.printstacktrace(); } { if (stream != null) { try { stream.close(); } catch (ioexception e) { e.printstacktrace(); } } if (fos != null) { try { fos.close(); } catch (ioexception e) { e.printstacktrace(); } } } bundle extras = intent.getextras(); if (extras != null) { messenger messenger = (messenger) extras.get("messenger"); message msg = message.obtain(); msg.arg1 = result; msg.obj = output.getabsolutepath(); try { messenger.send(msg); } catch (android.os.remoteexception e1) { log.w(getclass().getname(), "exception sending message", e1); } } } }
in above code used file streams & input stream reader downloading when tried download html file complete file downloaded sdcard.but when tried apk. file downloaded of 2.2 mb instead of 2.4 mb parsing problem there. kindly me resolve issue.
try piece of code :
url url = new url(fileurl); urlconnection connection = url.openconnection(); connection.connect(); int filelength = connection.getcontentlength(); inputstream input = new bufferedinputstream(url.openstream()); outputstream output = new fileoutputstream(output.getpath()); byte data[] = new byte[1024]; int count; while ((count = input.read(data)) != -1) { output.write(data, 0, count); } output.flush(); input.close(); result = activity.result_ok;
Comments
Post a Comment