2016-07-01 11 views
0

ich den folgenden Code haben große Dateien aus dem Internet herunterladen:Android-Downloads beschädigt

   try { 
        url = new URL(IPClass.SERVERIP + path[0].get(i) + "/" + fileName); 

        connection = (HttpURLConnection) url.openConnection(); 
        connection.setDoInput(true); 
        connection.setDoOutput(true); 
        connection.setReadTimeout(7000); 
        connection.connect(); 
        fileLengthOnServer = connection.getContentLength(); 

        input = connection.getInputStream(); 
        output = new FileOutputStream("/sdcard/Android/appdata/tmp/downloadtmp/" + fileName, true); 

        byte data[] = new byte[1024]; 
        int count; 

        while ((count = input.read(data)) != -1 && applicationDownloadList.containsKey(fileName)) { 
         total = total + count; 
         output.write(data, 0, count); 
         dl_progress = (int) ((total * 100L)/fileLengthOnServer); 
         applicationDownloadList.put(fileName, dl_progress); 

         if (total == fileLengthOnServer) { 
          downloadSuccessful(); 
         } 
        } 
       } catch (Exception e) { 
        if (applicationDownloadList.containsKey(fileName)) { 
         doInBackground(appPathList); 
        } 
       } finally { 
        try { 
         if (output != null) output.close(); 
         if (input != null) input.close(); 
        } catch (IOException ignored) { 
        } 
        if (connection != null) { 
         connection.disconnect(); 
        } 
       } 

Aber manchmal nicht immer die Datei-Downloads beschädigt! Können Sie mir bitte Lösungen geben, um einen erfolgreichen Download zu garantieren!

Antwort

0

Sie können Sync Adapter für große Downloads bevorzugen. Dadurch wird Ihre Datei heruntergeladen, obwohl Ihre App läuft oder nicht. Sie können auch eine Benachrichtigung über den Fortschritt des Downloads anzeigen.