以编程方式从android中的assets文件夹安装apk

时间:2012-10-01 12:19:19

标签: android android-intent android-install-apk

我正在尝试以编程方式从资源文件夹安装apk但不成功,请帮助我。我正在使用以下代码。谢谢。

Intent intent = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("file:///android_asset/youtuberanjit.apk"))
.setType("application/vnd.android.package-archive");
startActivity(intent);

2 个答案:

答案 0 :(得分: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) { }

答案 1 :(得分:-4)

将apk文件复制到assets文件夹中。

尝试这种方法对我有用..........

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