博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android中安装asset文件下的apk
阅读量:4259 次
发布时间:2019-05-26

本文共 1034 字,大约阅读时间需要 3 分钟。

在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);}

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

`

转载地址:http://zjaei.baihongyu.com/

你可能感兴趣的文章
常见聚合函数
查看>>
简单子查询
查看>>
联表查询
查看>>
关于WindowListener的使用
查看>>
关于KeyListener的简单使用
查看>>
关于鼠标移动监听接口:MouseMotionListener
查看>>
TCP/IP详解笔记(一)
查看>>
501. Find Mode in Binary Search Tree
查看>>
504. Base 7
查看>>
593. Valid Square
查看>>
494. Target Sum
查看>>
463. Island Perimeter
查看>>
TCP协议粗析
查看>>
653. Two Sum IV - Input is a BST
查看>>
spark rdd 和 DF 转换
查看>>
RDD 基础操作
查看>>
RDD基本操作(下)
查看>>
##########(python 解析参数方法 可用) Python optionParser模块的使用方法 #######
查看>>
org.apache.hadoop.io.compress系列1-认识解码器/编码器
查看>>
pyspark-combineByKey详解
查看>>