android - download errors, can't open files -


my app has connect google drive. connection works fine. can see files in drive. download of files works fine. unfortunately when try open files corrupted or can't open them @ all. know solution problem ??

enter code here

url url = new url(fileurl);  httpurlconnection httpconn = (httpurlconnection) url.openconnection(); int responsecode = httpconn.getresponsecode();  // check http response code first if (responsecode == httpurlconnection.http_ok) {     string filename = "";     string disposition = httpconn.getheaderfield("content-disposition");     string contenttype = httpconn.getcontenttype();     int contentlength = httpconn.getcontentlength();      if (disposition != null) {         // extracts file name header field         int index = disposition.indexof("filename=");         if (index > 0) {             filename = disposition.substring(index + 10,                     disposition.length() - 1);         }     } else {         // extracts file name url         filename = fileurl.substring(fileurl.lastindexof("/") + 1,                 fileurl.length());     }     system.out.println("content-type = " + contenttype);     system.out.println("content-disposition = " + disposition);     system.out.println("content-length = " + contentlength);     system.out.println("filename = " + filename);     filename = mr.gettitle();     // opens input stream http connection     // urlconnection connection = url.openconnection();     string savefilepath = savedir + file.separator + filename;       inputstream inputstream = httpconn.getinputstream();      fileoutputstream outputstream = new      fileoutputstream(savefilepath);     // opens output stream save file        int bytesread = 0;     // int read;     byte[] buffer = new byte[16384];     // while ((bytesread = inputstream.read(buffer)) > 0) {     // outputstream.write(buffer, 0, bytesread);     // }     while ((bytesread = inputstream.read(buffer)) != -1) {         outputstream.write(buffer, 0, bytesread);     }       outputstream.flush();        outputstream.close();      inputstream.close();      system.out.println("file downloaded"); } else {     system.out             .println("no file download. server replied http code: "                     + responsecode); } httpconn.disconnect(); 

it's problem between file length , byte buffer. quickly, please change , retry

byte[] buffer = new byte[1024]; 

or length of input stream create buffer

long streamlength = inputstream.available(); byte[] buffer = new byte[streamlength]; 

have fun!


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -