android - Unable to read file from server -
i want download file server. file created in android device file empty file. not writing in file.
my filereading code follows:
url url = new url(urlstring); httpurlconnection urlconnection = (httpurlconnection) url.openconnection(); urlconnection.setrequestmethod("get"); urlconnection.setdooutput(true); //connect urlconnection.connect(); file sdcardroot = environment.getexternalstoragedirectory(); //create new file, save downloaded file file file = new file(sdcardroot,pos); fileoutputstream fileoutput = new fileoutputstream(file); //stream used reading data internet inputstream inputstream = urlconnection.getinputstream(); //this total size of file downloading totalsize = urlconnection.getcontentlength(); //create buffer... byte[] buffer = new byte[1024]; int bufferlength = 0; while ( (bufferlength = inputstream.read(buffer)) > 0 ) { fileoutput.write(buffer, 0, bufferlength); downloadedsize += bufferlength; } fileoutput.close(); }
i have debugged code. there wrong in while ( (bufferlength = inputstream.read(buffer)) > 0 ) condition compiler didnt go inside loop.
my log cat error:
error/activitythread(8218): activity com.sec.android.app.myfiles.detailsactivity has leaked intentreceiver com.sec.android.app.myfiles.detailsactivity$4@42760570 registered here. missing call unregisterreceiver()? android.app.intentreceiverleaked: activity com.sec.android.app.myfiles.detailsactivity has leaked intentreceiver com.sec.android.app.myfiles.detailsactivity$4@42760570 registered here. missing call unregisterreceiver()? @ android.app.loadedapk$receiverdispatcher.<init>(loadedapk.java:792) @ android.app.loadedapk.getreceiverdispatcher(loadedapk.java:593) @ android.app.contextimpl.registerreceiverinternal(contextimpl.java:1254) @ android.app.contextimpl.registerreceiver(contextimpl.java:1241) @ android.app.contextimpl.registerreceiver(contextimpl.java:1235) @ android.content.contextwrapper.registerreceiver(contextwrapper.java:372) @ com.sec.android.app.myfiles.detailsactivity.setlocalereceiver(detailsactivity.java:185) @ com.sec.android.app.myfiles.detailsactivity.oncreate(detailsactivity.java:121) @ android.app.activity.performcreate(activity.java:5206) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1083) @ android.app.activitythread.performlaunchactivity(activitythread.java:2064) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2125) @ android.app.activitythread.access$600(activitythread.java:140) @ android.app.activitythread$h.handlemessage(activitythread.java:1227) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:4898) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1006) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:773) @ dalvik.system.nativestart.main(native method)
use asynctask download file server code download file server progress dialog , works fine.. please try it..
package .....; import java.io.bufferedinputstream; import java.io.file; import java.io.fileoutputstream; import java.io.inputstream; import java.io.outputstream; import java.net.url; import java.net.urlconnection; import android.app.activity; import android.app.dialog; import android.app.progressdialog; import android.os.asynctask; import android.os.bundle; import android.os.environment; import android.util.log; import android.view.view; import android.widget.button; import android.widget.edittext; public class facebook1 extends activity { button btnshowprogress; private progressdialog pdialog; public static final int progress_bar_type = 0; private static string file_url = "yoururl"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.layout); btnshowprogress = (button) findviewbyid(r.id.submit1); btnshowprogress.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { new downloadfilefromurl().execute(file_url); } }); } @override protected dialog oncreatedialog(int id) { switch (id) { case progress_bar_type: pdialog = new progressdialog(this); pdialog.setmessage("please wait....."); pdialog.setindeterminate(false); pdialog.setmax(100); pdialog.setprogressstyle(progressdialog.style_horizontal); pdialog.setcancelable(true); pdialog.show(); return pdialog; default: return null; } } class downloadfilefromurl extends asynctask<string, string, string> { protected static final int buffer_size = 0; @suppresswarnings("deprecation") @override protected void onpreexecute() { super.onpreexecute(); showdialog(progress_bar_type); } /** * downloading file in background thread * */ @override protected string doinbackground(string... f_url) { int count; try { url url = new url(f_url[0]); urlconnection conection = url.openconnection(); conection.connect(); int lenghtoffile = conection.getcontentlength(); inputstream input = new bufferedinputstream(url.openstream()); file downloadloc = environment.getexternalstoragedirectory(); outputstream output = new fileoutputstream(downloadloc + "/filenam.ext"); //type name of file extension. byte data[] = new byte[1024]; long total = 0; while ((count = input.read(data)) != -1) { total += count; publishprogress("" + (int) ((total * 100) / lenghtoffile)); output.write(data, 0, count); } output.flush(); output.close(); input.close(); } catch (exception e) { log.e("error: ", e.getmessage()); } return null; } protected void onprogressupdate(string... progress) { pdialog.setprogress(integer.parseint(progress[0])); } @override protected void onpostexecute(string file_url) { dismissdialog(progress_bar_type); }} }
manifest file:
<uses-permission android:name="android.permission.write_external_storage" > </uses-permission>
Comments
Post a Comment