java - Android - Download File From The Internet to SDCard - Safest Approach -


i'm developing app needs download file internet , store on sdcard. i've noticed devices report errors while downloading, such "parsing error". i'm assuming devices don't have sdcard or path i'm getting in class isn't correct. what's safest approach support devices if there's no sdcard or isn't mounted ? code:

/**          * 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();                 // getting file length                 int lenghtoffile = conection.getcontentlength();                  // input stream read file - 8k buffer                 inputstream input = new bufferedinputstream(url.openstream(), 8192);                  // output stream write file                 outputstream output = new fileoutputstream(environment.getexternalstoragedirectory().getpath()+"/download/file.apk");                  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;         } 

i think problem may on line:

outputstream output = new fileoutputstream(environment.getexternalstoragedirectory().getpath()+"/download/file.apk"); 

should use getexternalstoragedirectory() , download ? or there "safest" location common devices ?

first, don't want use asynctask download file. because if user kills screen hosts task, download killed too. intentservice.

second, familiarize android code examples here: http://developer.android.com/guide/topics/data/data-storage.html#filesexternal

you can check what's available , appropriate directory.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -