以编程方式安装位于/ res/raw中的.apk文件

Jef*_*ker 2 android

我已经为我的/ res/raw目录中的另一个应用程序存储了一个.apk文件,并希望为用户提供安装它的提示.这是我目前的代码:

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);
Run Code Online (Sandbox Code Playgroud)

但是,当意图运行时,我得到一个Parse错误: There is a problem parsing the package.

当运行其他应用程序时,.apk文件是由/ bin目录中的eclipse创建的文件.

如何正确完成此程序化本地安装?

Jef*_*ker 6

我想到了.我最终将我的.apk文件放在/ assets目录中,并以编程方式将其复制到我安装它的SD卡中.这是我的代码:

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
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助其他人有类似的问题!