Android:下载的文件大小小于其实际大小

时间:2012-10-24 10:29:07

标签: android

我想自动更新我的申请 这是我正在使用的代码

public void Update(String apkurl){
      try {
            URL url = new URL(apkurl);
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            String PATH = Environment.getExternalStorageDirectory() + "/download/";
            File file = new File(PATH);
            file.mkdirs();
            File outputFile = new File(file, "DeliverReceipt.apk");
            FileOutputStream fos = new FileOutputStream(outputFile);

            InputStream is = c.getInputStream();

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            }
            fos.close();
            is.close();//till here, it works fine - .apk is download to my sdcard in download file

            /*Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/apk/" + "DeliverReceipt.apk")), "application/vnd.android.package-archive");
            startActivity(intent);  //installation is not working        
            */
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(), "Update error!", Toast.LENGTH_LONG).show();
        }
  }

下载的文件大小只有20kb,比原来的小 我该如何解决这个问题呢? 谢谢你 *注意:如果我在浏览器中尝试这个URL它的工作,可以下载apk

1 个答案:

答案 0 :(得分:0)

在使用fos.flush()

关闭文件之前,您应该刷新