Android中安装asset文件下的apk

来源:互联网 发布:深圳行知小学怎么样 编辑:程序博客网 时间:2024/05/18 22:46

在Android的项目中,有时需要从asset文件夹下复制APK文件到内存当中去安装,下面的函数就是实现了这样的功能。
`
//安装APK
private void installAPK() {
String path=Environment.getExternalStorageDirectory().getAbsolutePath()
+File.separator+getPackageName()+File.separator+”apk”;
InputStream is=null;
FileOutputStream fos =null;
File fileapk = null;
try{
is = getAssets().open(“xxxx.apk”);
File file = new File(path);
if(!file.exists()){
file.mkdirs();
}
fileapk=new File(file, “xxxx.apk”);
fileapk.createNewFile();
fos= new FileOutputStream(fileapk);
byte[] temp = new byte[1024];
int i = 0;
while ((i = is.read(temp)) > 0) {
fos.write(temp, 0, i);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(fos!=null){
fos.close();
}
if(fos!=null){
fos.close();
}
}catch(Exception e){
}
}

    Intent intent = new Intent();    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    intent.setAction(Intent.ACTION_VIEW);    intent.setDataAndType(Uri.fromFile(new File(fileapk.getPath())),               "application/vnd.android.package-archive");      startActivity(intent);}

本人菜鸟一个,欢迎各位大神指导!
`

0 0