http - Android - Download and Open Pdf -


i have application downloads , opens pdf listview click listener.

a file downloads , save phone has file size of 0 bytes therefore can not open. used tutorial code http://www.coderzheaven.com/2013/03/06/download-pdf-file-open-android-installed-pdf-reader/ here code. appreciated.

public class openpdfactivity extends activity {      textview tv_loading;     string dest_file_path = "pdf-test.pdf";     int downloadedsize = 0, totalsize;     string download_file_url = "http://www.education.gov.yk.ca/pdf/pdf-test.pdf";     float per = 0;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         tv_loading = new textview(this);         setcontentview(tv_loading);         tv_loading.setgravity(gravity.center);         tv_loading.settypeface(null, typeface.bold);         downloadandopenpdf();     }      void downloadandopenpdf() {         new thread(new runnable() {             public void run() {                 uri path = uri.fromfile(downloadfile(download_file_url));                 try {                     intent intent = new intent(intent.action_view);                     intent.setdataandtype(path, "application/pdf");                     intent.setflags(intent.flag_activity_clear_top);                     startactivity(intent);                     finish();                 } catch (activitynotfoundexception e) {                     tv_loading                             .seterror("pdf reader application not installed in device");                 }             }         }).start();      }      file downloadfile(string dwnload_file_path) {         file file = null;         try {              url url = new url(dwnload_file_path);             httpurlconnection urlconnection = (httpurlconnection) url                     .openconnection();              urlconnection.setrequestmethod("get");             urlconnection.setdooutput(true);              // connect             urlconnection.connect();              // set path want save file             file sdcardroot = environment.getexternalstoragedirectory();             // create new file, save downloaded file             file = new file(sdcardroot, dest_file_path);              fileoutputstream fileoutput = new fileoutputstream(file);              // stream used reading data internet             inputstream inputstream = urlconnection.getinputstream();              // total size of file             // downloading             totalsize = urlconnection.getcontentlength();             settext("starting pdf download...");              // create buffer...             byte[] buffer = new byte[1024 * 1024];               int bufferlength = 0;              while ((bufferlength = inputstream.read(buffer)) > 0) {                 fileoutput.write(buffer, 0, bufferlength);                 downloadedsize += bufferlength;                 per = ((float) downloadedsize / totalsize) * 100;                 settext("total pdf file size  : "                         + (totalsize / 1024)                         + " kb\n\ndownloading pdf " + (int) per                         + "% complete");             }             // close output stream when complete //             fileoutput.close();             settext("download complete. open pdf application installed in device.");          } catch (final malformedurlexception e) {             settexterror("some error occured. press , try again.",                     color.red);         } catch (final ioexception e) {             settexterror("some error occured. press , try again.",                     color.red);         } catch (final exception e) {             settexterror(                     "failed download image. please check internet connection.",                     color.red);         }         return file;     }      void settexterror(final string message, final int color) {         runonuithread(new runnable() {             public void run() {                 tv_loading.settextcolor(color);                 tv_loading.settext(message);             }         });      }      void settext(final string txt) {         runonuithread(new runnable() {             public void run() {                 tv_loading.settext(txt);             }         });      }  } 

a couple of things:

maybe add timeout?

try not check content length now.

use bufferedoutputstream

something this:

fileoutputstream fos = new fileoutputstream(file);  bufferedoutputstream out = new bufferedoutputstream( fos);  try { httpurlconnection urlconnection = (httpurlconnection) url                     .openconnection();  urlconnection.setrequestmethod("get");   urlconnection.setreadtimeout( 15000);   urlconnection.connect();  try {        inputstream in = urlconnection.getinputstream();        byte[] buffer = new byte[1024 * 1024];        int len = 0;         while (( len = in.read( buffer)) > 0)        {          out.write( buffer, 0, len);        }        out.flush();      } {        fos.getfd(). sync();        out.close();      }      } catch (ioexception eio) {  log.e("download tag", "exception in download", eio); } 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -