本文共 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/