4

I am trying to install apk programatically from assets folder but not success, Please help me. I am using following code for that. thank you.

Intent intent = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("file:///android_asset/youtuberanjit.apk"))
.setType("application/vnd.android.package-archive");
startActivity(intent);
Ranjitsingh Chandel
  • 1,479
  • 6
  • 19
  • 33

2 Answers2

15
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) { }
CAMOBAP
  • 5,523
  • 8
  • 58
  • 93
skygeek
  • 1,548
  • 11
  • 24
  • but we need to install apk directly from assets folder. Is this possible? please help me... – Ranjitsingh Chandel Oct 01 '12 at 12:21
  • yes this code is for that purpose to install from assets folder! – skygeek Oct 01 '12 at 12:22
  • But this code first copy apk from asset folder and saved in sdcard... and then access from sdcard not directly assets... I want to install directly from assets to device. because sdcard not available in my device to check this code. please help me this way.. – Ranjitsingh Chandel Oct 01 '12 at 12:27
  • you can delete the file after copying – skygeek Oct 01 '12 at 12:32
  • Is this possible without using sdcard ? – Ranjitsingh Chandel Oct 01 '12 at 12:38
  • or you can do it this way intent.setDataAndType(Uri.fromFile("file:///android_asset/myapk.apk"), "application/vnd.android.package-archive"); I have not tested this! – skygeek Oct 01 '12 at 12:42
  • 1
    this one is correct intent.setDataAndType(Uri.fromFile(new File("file:///android_asset/myapk.apk")), "application/vnd.android.package-archive"); – skygeek Oct 01 '12 at 12:44
  • its regarding to your .apk file. cross check version code name package name and all – skygeek Oct 01 '12 at 13:08
  • Using this code first I saved apk in data/data/my_application_pkg/myapk.apk Then install apk from this place using INTENT. Is successfully done. Thank you very much for all answers and reply... – Ranjitsingh Chandel Oct 02 '12 at 12:25
  • Hi Ranjit, Can u please explain how u saved the file in the internal memory and den installed it... Coz i did the same thing, but still getting the "problem parsing the package error"... Have i left out somthing?? Please if you can help.. – Gautam Mandsorwale Nov 05 '12 at 06:35
  • this is my internal path `/data/data/com.example.abc/files/myApkName.apk` – Gautam Mandsorwale Nov 05 '12 at 06:39
  • 1
    @RanjitChandel Hi,,I m also getting same parsing error..I am saving the apk in data/data/my_application_pkg/myapk.apk and then installing but getting the parsing error..Can u please help me out to resolve this issue... – user1969053 Mar 06 '13 at 10:01
  • 1
    @RanjitChande Hi, I am getting **Parse error**. Saved the apk in application dir( /data/data//files/ . My device OS is Marshmallow. – Miguel A. Mar 21 '17 at 15:06
-4

Copy the apk file into assets folder.

try this method it worked for me ..........

private void launchComponent(String packageName, String name) { 

  // Name should be starting activity complete name with package name
        Intent launch_intent = new Intent("android.intent.action.MAIN");
        launch_intent.addCategory("android.intent.category.LAUNCHER");
        launch_intent.setComponent(new ComponentName(packageName, name));
        launch_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        this.startActivity(launch_intent);
    }
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
rgu
  • 43
  • 5