How to install apk file programmatically

I want to install apk file from my application.

I created an application containing a button, when I click this button, then another apk that I saved in the resources folder must be installed,
Here is what I did:

public void onClick(View v) { // Intent intent = new Intent("com.google.zxing.client.android.SCAN"); // intent.setPackage("com.google.zxing.client.android"); // intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); // startActivityForResult(intent, 0); File file = new File("android.resource://com.app.barcodescanner/raw", "scan.apk"); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); startActivity(intent); } 

any ideas? Please help me with this.

+6
android
source share
3 answers
 Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File (Environment.getExternalStorageDirectory() + "/barcode.apk")), "application/vnd.android.package-archive"); startActivity(intent); 
+14
source share

It will probably not work with android.resource Uri . Try copying the APK to external storage and install there.

+2
source share
 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); File DbFile=new File("mnt/sdcard/HelloAndroid.apk"); if(!(DbFile.exists())) { try { int length = 0; DbFile.createNewFile(); InputStream inputStream = this.getAssets().open("HelloAndroid.apk"); FileOutputStream fOutputStream = new FileOutputStream(DbFile); byte[] buffer = new byte[inputStream.available()]; while ((length = inputStream.read(buffer)) > 0) { fOutputStream.write(buffer, 0, length); } fOutputStream.flush(); fOutputStream.close(); inputStream.close(); } catch (Exception ex) { System.out.println("Error in creating new database at mobile..." + ex); ex.printStackTrace(); } } Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File("/mnt/sdcard/HelloAndroid.apk")), "application/vnd.android.package-archive"); startActivity(intent); } 

Here I saved the apk file in the resource folder. You can try this.

0
source share

All Articles