install Apk From Assets - Android App

Android examples for App:APK Install and Uninstall

Description

install Apk From Assets

Demo Code


//package com.java2s;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;

import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static boolean installApkFromAssets(Context context,
            String apkName) {//from  w ww .j  a  v a  2  s. c o m
        AssetManager assetManager = context.getAssets();
        InputStream in = null;
        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