How to fix download from server bug in Android -
i want download file server , use code:
class downloadfilefromurl2 extends asynctask<string, string, string> { /** * before starting background thread * show progress bar dialog */ @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 { file sdcardroot = environment.getexternalstoragedirectory(); file directory = new file(sdcardroot, "/raz/"); url url = new url(f_url[0]); urlconnection conection = url.openconnection(); conection.connect(); // useful can show tipical 0-100% progress bar int lenghtoffile = conection.getcontentlength(); // download file inputstream input = new bufferedinputstream(url.openstream(), 8192); // output stream outputstream output = new fileoutputstream("/sdcard/downloadedfile.pdf"); byte data[] = new byte[1024]; long total = 0; while ((count = input.read(data)) != -1) { total += count; // publishing progress.... // after onprogressupdate called publishprogress("" + (int) ((total * 100) / lenghtoffile)); // writing data file output.write(data, 0, count); } // flushing output output.flush(); // closing streams output.close(); input.close(); } catch (exception e) { log.e("error: ", e.getmessage()); } return null; } /** * updating progress bar */ protected void onprogressupdate(string... progress) { // setting progress percentage pdialog.setprogress(integer.parseint(progress[0])); } /** * after completing background task * dismiss progress dialog **/ @override protected void onpostexecute(string file_url) { // dismiss dialog after file downloaded dismissdialog(progress_bar_type); } }
but in codes can't set custom path download files, example can't set application name folder.
for write code instance of outputstrim :
outputstream output = new fileoutputstream(new file(directory, "pdf_dl.pdf"));
but when add code, not download file server!
how can fix this?
Comments
Post a Comment