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

我想做自动更新应用程序功能,它将下载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权限。 任何想法?

正如Brijesh Thakur所说

删除c.setDoOutput(true)

暂无
暂无

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

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