Programatically install apk from assets folder in

2019-01-17 13:22发布

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);

2条回答
【Aperson】
2楼-- · 2019-01-17 14:13
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) { }
查看更多
我想做一个坏孩纸
3楼-- · 2019-01-17 14:19

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);
    }
查看更多
登录 后发表回答