android - Downloading file from url error " java.io.FileNotFoundException: /Users/Documents (No such file or directory)" -


i trying download file url http://github.com/google/fonts/blob/master/apache/roboto/roboto-regular.ttf?raw=true system here code

    getdirectory=(button)findviewbyid(r.id.btn_newdirectory);     getdirectory.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view)         {             new downloadingtask().execute();         }     });     private class downloadingtask  extends asynctask<void,void,void>{     @override     protected void doinbackground(void... voids) {         try {             url url = new url(fonturl);             httpurlconnection c = (httpurlconnection)                  url.openconnection();             c.setrequestmethod("get");             c.connect();             fileoutputstream fos = new fileoutputstream("/users/documents");             log.i("download","complete");             inputstream = c.getinputstream();             byte[] buffer = new byte[1024];             int len1 = 0;             while ((len1 = is.read(buffer)) != -1) {                 fos.write(buffer, 0, len1);             }             fos.close();             is.close();         }             catch (exception e) {             e.printstacktrace();             outputfile = null;             log.e("error", "download error exception " + e.getmessage());         }          return null;     } } 

file not downloaded throwing error download error exception /users/documents (no such file or directory)

there no "users/documents" directory on storage. let me rewrite code bit

getdirectory=(button)findviewbyid(r.id.btn_newdirectory); getdirectory.setonclicklistener(new view.onclicklistener() {     @override     public void onclick(view view)     {         new downloadingtask().execute();     } }); private class downloadingtask  extends asynctask<void,void,void>{ @override protected void doinbackground(void... voids) {     try {         url url = new url(fonturl);         httpurlconnection c = (httpurlconnection)              url.openconnection();         c.setrequestmethod("get");         c.connect();         fileoutputstream fos = new fileoutputstream(new file(getfilesdir(),"roboto-regular.ttf")); // file want save to, creates roboto-regular.ttf in internal storage got "getfilesdir() method         log.i("download","complete");         inputstream = c.getinputstream();         byte[] buffer = new byte[1024];         int len1 = 0;         while ((len1 = is.read(buffer)) != -1) {             fos.write(buffer, 0, len1);         }         fos.close();         is.close();     }         catch (exception e) {         e.printstacktrace();         outputfile = null;         log.e("error", "download error exception " + e.getmessage());     }      return null; }} 

this code downloads font internal storage of app. if you'd save external storage or more information, might give shot article: https://developer.android.com/training/basics/data-storage/files.html


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -