Android:How to download the File from the server and save it in specific folder in sdcard -
i have 1 requirement in android application. need download , save file in specific folder of sdcard programmatically. have developed source code..
and code
string downloadurl = "http://myexample.com/android/"; string filename = "myclock_db.db"; downloaddatabase(downloadurl,filename); // , method public void downloaddatabase(string downloadurl, string filename) { try { file root = android.os.environment.getexternalstoragedirectory(); file dir = new file(root.getabsolutepath() + "/myclock/databases"); if(dir.exists() == false){ dir.mkdirs(); } url url = new url("http://myexample.com/android/"); file file = new file(dir,filename); long starttime = system.currenttimemillis(); log.d("downloadmanager" , "download url:" +url); log.d("downloadmanager" , "download file name:" + filename); urlconnection uconn = url.openconnection(); uconn.setreadtimeout(timeout_connection); uconn.setconnecttimeout(timeout_socket); inputstream = uconn.getinputstream(); bufferedinputstream bufferinstream = new bufferedinputstream(is); bytearraybuffer baf = new bytearraybuffer(5000); int current = 0; while((current = bufferinstream.read()) != -1){ baf.append((byte) current); } fileoutputstream fos = new fileoutputstream( file); fos.write(baf.tobytearray()); fos.flush(); fos.close(); log.d("downloadmanager" , "download ready in" + ((system.currenttimemillis() - starttime)/1000) + "sec"); int dotindex = filename.lastindexof('.'); if(dotindex>=0){ filename = filename.substring(0,dotindex); } catch(ioexception e) { log.d("downloadmanager" , "error:" + e); } }
now issue empty file filename myclock_db.db saving in path. need download , save content of file in specific folder. tried several ways file download, can't. can me solve issue...
thanks in advance...
your download url not link file. it's directory. make sure file , exists. check logcat window error logs. 1 more suggestion, better printstacktrace() in catch blocks instead of logs. gives more detailed view of error.
change line:
url url = new url("http://myexample.com/android/");
to:
url url = new url("http://myexample.com/android/yourfilename.txt"); //some file url
next, in catch block, add line:
e.printstacktrace();
also in directory path, should this:
file dir = new file(root.getabsolutepath() + "/mnt/sdcard/myclock/databases");
instead of
file dir = new file(root.getabsolutepath() + "/myclock/databases");
next, make sure have acquired permission writing external storage in android manifest.
Comments
Post a Comment