我正在构建一个依赖于另一个应用程序的android应用程序,实际上另一个应用程序是我要与该应用程序一起安装的书写板,我试图将apk文件放在原始文件夹中并尝试像这样访问它:

      Uri app= Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.app);


可行吗如果是,如何安装此应用程序?

最佳答案

将您的apk放置在资产文件夹中,并将myAPKFile.apk更改为您的apk文件名,然后使用此功能安装您的apk。

所需权限:

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


private boolean InstallmyAPK(){

AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File myAPKFile = new File(Environment.getExternalStorageDirectory().getPath()+"/myAPKFile.apk");

try {

    if(!myAPKFile.exists()){
        in = assetManager.open("myAPKFile.apk");
        out = new FileOutputStream(myAPKFile);

        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(myAPKFile),
            "application/vnd.android.package-archive");
    startActivity(intent);
    return true;

} catch(Exception e) {return false;}

}

07-25 22:42
查看更多