小笔记---从asset中取出apk,然后安装。

来源:互联网 发布:龙胜温泉网络售票 编辑:程序博客网 时间:2024/06/02 00:14

       /**
* 将apk的assets目录中的apk复制到sdcard,然后安装。
* @param context
* @param apkName
*/
private void installApk(Context context, String apkName){
try {
FileOutputStream out = new FileOutputStream("/sdcard/data/data/apk2.apk");
InputStream in = context.getResources().getAssets().open(apkName);
byte[] buffer = new byte[1024];
int length = in.read(buffer);
while(length > 0){
out.write(buffer);
System.out.println(length);
length = in.read(buffer);
}
Intent intent = new Intent(Intent.ACTION_VIEW);  
             intent.setDataAndType(Uri.fromFile(new File("/sdcard/data/data/apk2.apk")),   
                     "application/vnd.android.package-archive");  
context.startActivity(intent);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

原创粉丝点击