今天在做自动检测软件更新的时候,将服务器上apk下载下来后,调用Android安装代码进行安装,报了如下错误:

解析错误:解析软件包时出现问题





检查了一下代码,发现出现这个问题的原因是没有关闭输入输出流和关闭网络连接,在下载完后需要依次将这些打开的流关闭,就能解决问题了

 bis.close();
 bos.close();
 connection.disconnect();


/** * 下载apk */public void downLoadApk() {    try {        URL url = new URL("http://hxyiyo.com/ydjx_tV1.0.apk");        HttpURLConnection connection = (HttpURLConnection) url.openConnection();        BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());        File file = new File(Environment.getExternalStorageDirectory() + "/newap.apk");        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));        int fileLength = connection.getContentLength();        int downloadLength = 0;        int progressLength = 0;        int n = 0;        byte[] buffer = new byte[1024];        while ((n = bis.read(buffer, 0, buffer.length)) != -1) {            bos.write(buffer, 0, n);            downloadLength += n;            progressLength = (int)(((float)downloadLength / fileLength)*100);            Message msg = new Message();            msg.arg1 = progressLength;            hanlder.sendMessage(msg);        }        bis.close();        bos.close();        connection.disconnect();    } catch (MalformedURLException e) {        e.printStackTrace();    } catch (IOException e) {        e.printStackTrace();    }}


更多相关文章

  1. 代码在android 8 上面正常运行在 android 9 上面却不能联网问题
  2. Android(安卓)内存溢出解决方案(OOM) 整理总结
  3. eclipse Alt+/ 代码失灵、不响应的解决办法
  4. android 图片旋转 反锯齿
  5. android开发之Seekbar
  6. 第15章、布局Layouts之TableLayout表格布局(从零开始学Android)
  7. C++开发安卓、windows下搭建Android(安卓)NDK开发环境
  8. Android使用意图链接活动和意图筛选器
  9. 系统学习android开发技术 android三部曲

随机推荐

  1. PHP MYSQL无法在While或For循环中获取数
  2. MySQL的ibdata1文件占用过大瘦身
  3. 数据库keepalived部署文档
  4. 为什么我们的查询卡在MySql中的“写入网
  5. linux环境下写C++操作mysql(一)
  6. 蜂巢和数据库之间的完整性检查
  7. sql随机获获取数据
  8. 替换wordpress WP_POSTS表中post_date字
  9. mysql的count方法详解
  10. Spring+SpringMVC+MyBatis+easyUI整合基