How to download and automatic install apk from url in android -


i'm trying programmatically download .apk file given url , install it, getting filenotfoundexception. possible reason issue?

        try {                 url url = new url(fileurl);                 httpurlconnection c = (httpurlconnection) url.openconnection();                 c.setrequestmethod("get");                 c.setdooutput(true);                 c.connect();                  string path = "/mnt/sdcard/download/";                 file file = new file(path);                 file.mkdirs();                 file outputfile = new file(file, "versionupdate.apk");                 if(outputfile.exists()){                     outputfile.delete();                 }                 fileoutputstream fos = new fileoutputstream(outputfile);  **//getting error in line**                   inputstream = c.getinputstream();                    byte[] buffer = new byte[1024];                 int len1 = 0;                 while ((len1 = is.read(buffer)) != -1) {                     fos.write(buffer, 0, len1);                 }                 fos.flush();                 fos.close();                 is.close();                 } catch (exception e) {                 log.e("updateapp", "update error! " + e.getmessage());             }             return null; }      @override         protected void onpostexecute(string unused) {             //dismiss dialog after file downloaded             dismissdialog(dialog_download_progress);             intent intent = new intent(intent.action_view);              intent.addflags(intent.flag_activity_new_task);             intent.setdataandtype(uri.parse("file:///sdcard/download/versionupdate.apk"),"application/vnd.android.package-archive");              startactivity(intent);         } 

try following code

 file outputfile = new file(file, "versionupdate.apk");  if(!outputfile.exists())  {     outputfile.createnewfile();  } 

what doing deleting file, when exist, fileoutputstream not file want download apk .

if file exist, fileoutputstream override content new update.

if have queries, ask!!


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -