簡體   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