4

I know I can install an Android application programmatically by the below code which passes the URI of an APK file. Can I install the application without passing an APK file URI? For example, getting the byte array of an APK file and installing it?

File appFile = new File("application.apk");
Intent installIntent = new Intent(Intent.ACTION_VIEW);
installIntent.setDataAndType(Uri.fromFile(appFile),"application/vnd.android.package-archive");
startActivity(installIntent);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Armin
  • 599
  • 2
  • 8
  • 19

1 Answers1

2

When you install an application this way, you aren't actually directly installing the application. What happens is that you start the installer and pass the installer a reference to the APK that you want to have installed. The installer isn't part of your application code and has no access to the memory in your application's process.

The only way to do this, if you have a byte array containing the APK, would be to write the byte array to a file and then start the installer and pass it a URI that points to the file that you've written. Once the installation is complete you could then delete the file (so as not to leave random garbage on the user's phone).

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Yes, alas, the installer program needs a file on the filesystem. Even using a `content://` `Uri` is insufficient: http://stackoverflow.com/questions/9637629/can-we-install-an-apk-from-a-contentprovider – CommonsWare Oct 26 '12 at 14:12
  • is there any way to directly installing the application ?! – Armin Oct 26 '12 at 14:38
  • No. This would be a security nightmare if an application could install whatever it wanted without user interaction. – David Wasser Oct 26 '12 at 14:50
  • can i write byte array to a file in phone memory and pass Uri that refrence to it to the installer ? – Armin Oct 26 '12 at 19:22
  • Yes. You can create an APK file on the phone memory and then pass a URI pointing to that to the installer. This will be a `file://` URL and not a `content://` URL. – David Wasser Oct 27 '12 at 21:59