Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: install .apk programmatically
    text
    copied!<p>I made this with help from <a href="https://stackoverflow.com/questions/576513/android-download-binary-file-problems">Android download binary file problems</a> and <a href="https://stackoverflow.com/questions/4604239/install-application-programmatically-on-android">Install Application programmatically on Android</a>.</p> <p>I want to make auto-update and auto-install at once. It is local so it's non-market application.</p> <p>Here is my code for it:</p> <pre><code>public void Update(String apkurl){ try { URL url = new URL(apkurl); HttpURLConnection c = (HttpURLConnection) url.openConnection(); c.setRequestMethod("GET"); c.setDoOutput(true); c.connect(); String PATH = Environment.getExternalStorageDirectory() + "/download/"; File file = new File(PATH); file.mkdirs(); File outputFile = new File(file, "app.apk"); FileOutputStream fos = new FileOutputStream(outputFile); InputStream is = 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();//till here, it works fine - .apk is download to my sdcard in download file Intent promptInstall = new Intent(Intent.ACTION_VIEW) .setData(Uri.parse(PATH+"app.apk")) .setType("application/android.com.app"); startActivity(promptInstall);//installation is not working } catch (IOException e) { Toast.makeText(getApplicationContext(), "Update error!", Toast.LENGTH_LONG).show(); } } </code></pre> <p>My permissions are <code>INTERNET</code>, <code>WRITE_EXTERNAL_STORAGE</code>, <code>INSTALL_PACKAGES</code>, and <code>DELETE_PACKAGES</code>.</p> <p>When Intent <code>promptInstall</code> is loaded, the app crashes =/</p> <p>So, am I missing permissions or is my code incorrect, or is there a better way to do this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload