Sunday, 14 October 2012

Install apk from assets android

  1. Copy/put your apk file in assets folder of your project
  2. Set onClick event of button to install apk. Copy and paste following code
  3. ===============================================
    public void loadMyApk()
    {
    copyAssets(); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/" + "BS.apk")), "application/vnd.android.package-archive"); startActivityForResult(intent, 4);
    }
    ===============================================
    private void copyAssets()
    {
    AssetManager assetManager = getAssets(); String[] files = null; try { files = assetManager.list(""); } catch (IOException e) { Log.e("tag", e.getMessage()); } for(String filename : files) { InputStream in = null; OutputStream out = null; try { //fileone=filename; in = assetManager.open(filename); out = new FileOutputStream("/sdcard/" + filename); copyFile(in, out); in.close(); in = null; out.flush(); out.close(); out = null; } catch(Exception e) { Log.e("tag", e.getMessage()); } }
    }
    ===============================================
    private void copyFile(InputStream in, OutputStream out) throws IOException
    {
    byte[] buffer = new byte[1024]; int read; while((read = in.read(buffer)) != -1){ out.write(buffer, 0, read); }
    }

    ===============================================
  4. Its done
In the above process I have copied apk file from assets folder to sdcard and installing from sdcard. If you want to delete from sdcard you can call delete() method for file

2 comments:

  1. You have a example application?

    ReplyDelete
  2. Yaa... but source code is too big. Very soon will share a demo. Thanks for asking.

    ReplyDelete