5
            URL url = new URL(arg0[1]);
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            String PATH = "/mnt/sdcard/Android/";
            File file = new File(PATH);
            file.mkdirs();
            File outputFile = new File(file, "myGame.apk");
            if(outputFile.exists()){
                outputFile.delete();
            }
            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();

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File("mnt/sdcard/Android/myGame.apk")), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);

我想做自动更新应用程序功能,它会下载 apk 文件并安装它。但是,当我通过 Java 下载并捕获异常时:java.io.FileNotFoundException http://192.168.2.143/myGame.apk,其中 url[0]="http://192.168.2.143/myGame.apk",我使用设备中的浏览器打开http://192.168.2.143/myGame.apk,可以下载 apk 并在 sdcard/Download 中/ 文件夹。我的清单中有 INTERNET 权限。任何想法?

4

1 回答 1

1

正如 Brijesh Thakur 所说

消除c.setDoOutput(true)

于 2016-02-26T10:27:48.667 回答