简体   繁体   English

在Android中使用HttpURLConnection的apk文件的FileNotFoundException

[英]FileNotFoundException for apk file with HttpURLConnection in Android

            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);

I want to do auto-update app function and it will download the apk file and install it. 我想做自动更新应用程序功能,它将下载apk文件并安装它。 However when I download through Java and exception caught: java.io.FileNotFoundException http://192.168.2.143/myGame.apk , where url[0]="http://192.168.2.143/myGame.apk", I use browser in my device to open http://192.168.2.143/myGame.apk , the apk can be downloaded and inside sdcard/Download/ folder. 但是当我通过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 /文件夹中。 I have INTERNET permission in my manifest. 我的清单中有INTERNET权限。 Any idea? 任何想法?

As Brijesh Thakur said 正如Brijesh Thakur所说

Remove c.setDoOutput(true) 删除c.setDoOutput(true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM