install Apk From Assets - Android android.content.pm

Android examples for android.content.pm:Apk Install

Description

install Apk From Assets

Demo Code

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;

public class Main {

  public static boolean installApkFromAssets(Context context, String apkName) {
    AssetManager assetManager = context.getAssets();
    InputStream in = null;/*  w  w  w .jav a  2  s  . c o m*/
    OutputStream out = null;

    try {
      in = assetManager.open(apkName);
      out = new FileOutputStream("/sdcard/" + apkName);

      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/" + apkName)), "application/vnd.android.package-archive");

      context.startActivity(intent);

      return true;
    } catch (Exception e) {
      return false;
    }
  }

}

Related Tutorials