0

我想自动更新我的应用程序
这是我正在使用的代码

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

4

1 回答 1

0

您可能应该在关闭文件之前刷新,使用fos.flush()

于 2012-10-24T11:23:59.447 回答