简体   繁体   中英

placing and running apk file inside android project

I am building an android app which depends on the other application ,actually the other application is the writing pad which I want to install with the application,I am trying to place the apk file inside the raw folder and trying to access it like this:

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

Is it feasible? If yes how can I install this app?

Place your apk in asset folder and change myAPKFile.apk to your apk file name and use this function to install your apk.

Permissions required:

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

}

try (I think this question has already been answered before here: Install Application programmatically on Android )

  Intent promptInstall = new Intent(Intent.ACTION_VIEW)
        .setDataAndType(Uri.parse("file:///path/to/your.apk"), 
                        "application/vnd.android.package-archive");
    startActivity(promptInstall); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM