2

I have stored a .apk file for another app in my /res/raw directory and wish to give the user a prompt to install it. Here's my current code:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("android.resource://" + getPackageName() + "/res/raw/myapk.apk")), "application/vnd.android.package-archive");
startActivity(intent);

However, when the intent runs, I get a Parse error: There is a problem parsing the package.

The .apk file is the one created by eclipse in the /bin directory when I run the other application.

How would I accomplish this programmatic local install correctly?

Jeff Gortmaker
  • 4,607
  • 3
  • 22
  • 29

3 Answers3

7

I figured it out. I ended up putting my .apk file in the /assets directory and copying it programatically to the sd card where I installed it from. Here's my code:

AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
try {
    in = assetManager.open("myapk.apk");
    out = new FileOutputStream("/sdcard/myapk.apk");
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
        out.write(buffer, 0, read);
    }
    in.close();
    in = null;
    out.flush();
    out.close();
    out = null;
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(new File("/sdcard/myapk.apk")), "application/vnd.android.package-archive");
    startActivity(intent);
}catch(Exception e){
    // deal with copying problem
}

Hopefully this will help someone else with a similar question!

Jeff Gortmaker
  • 4,607
  • 3
  • 22
  • 29
  • 1
    thanks! but better use Environment.getExternalStorageDirectory() rather than hard-coding "/sdcard" – ligi Jan 27 '14 at 10:52
0
String path = "file:///android_asset/raw/myapk.apk";

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");
startActivity(intent);
MAC
  • 15,799
  • 8
  • 54
  • 95
0

also add one permission in manifest file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

otherwise "permission denied" error occur.

ligi
  • 39,001
  • 44
  • 144
  • 244
Ranjitsingh Chandel
  • 1,479
  • 6
  • 19
  • 33