android - InputStream returns empty file when downloading a PDF -


so i'm trying download pdf file using httpurlconnection , think i've done right input- , outputstreams, yet when open downloaded pdf file (using built in file manager in android and/or adb), or inspect transferring os x, it's empty , size shows 0 bytes.

the site i'm trying download pdf is:

http://www.pdf995.com/samples/pdf.pdf

here's code

    public static void downloadfile(final string fileurl, final file directory) {      thread thread = new thread(new runnable(){         @override         public void run() {             try {                  try {                     fileoutputstream f = new fileoutputstream(directory);                     url u = new url(fileurl);                     c = (httpurlconnection) u.openconnection();                     c.setrequestmethod("get");                     //c.setrequestproperty("content-type", "application/pdf");                     //c.setdooutput(true);                     c.connect();                     log.d("debugz", integer.tostring(c.getcontentlength()));                      inputstream in = c.getinputstream();                      byte[] buffer = new byte[1024];                     int len1 = 0;                     while ((len1 = in.read(buffer)) > 0) {                         f.write(buffer, 0, len1);                     }                     f.flush();                     f.getfd().sync();                     f.close();                   } catch (exception e) {                     e.printstacktrace();                 }               } catch (exception e) {                 e.printstacktrace();             }         }     });      thread.start();   }   public static string getpdf(string pdfurl) {      string extstoragedirectory = environment.getexternalstoragedirectory()             .tostring();     file folder = new file(extstoragedirectory, "schematisktscheman");     folder.mkdir();     file file = new file(folder, "schemainfo.pdf");     try {         file.createnewfile();     } catch (ioexception e1) {         e1.printstacktrace();     }     downloadfile(pdfurl, file);      return null; //added return } 

in main activity:

schedulepdfprocessor.getpdf("http://www.pdf995.com/samples/pdf.pdf"); sendbroadcast(new intent(intent.action_media_scanner_scan_file, uri.parse("file://" + environment.getexternalstoragedirectory() + "/schematisktscheman/schemainfo.pdf"))); 

edit: ran network code on main thread, threw exception. creating new thread downloading , gets example pdf (http://www.pdf995.com/samples/pdf.pdf) , puts content in file fine. @greenapps!

first, replace:

f.close(); 

with:

f.flush(); f.getfd().sync(); f.close(); 

this ensures written disk before continuing.

then, need use mediascannerconnection , scanfile() mediastore know updated file.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -