2013-02-14 67 views
4

我使用AsyncTask從互聯網下載約50 MB文件。有時候,當我下載這個文件時,進度條增益非常慢(即使在我使用Wi-Fi時)。在一分鐘後,手機顯示我,下載完成,但文件本身只有〜100kB,沒有更多。但是當我重新啓動設備,並嘗試下載文件時,下載過程會短暫而快速地執行。有沒有人遇到同樣的問題?在下載新文件之前是否需要清除相同的下載內存?我正在將文件下載到Environment.externalStoryDirectory()。AsyncTask - 緩慢下載

THX

從活動中調用下載:

  mProgressDialog = new ProgressDialog(ItemDetails.this); 
      mProgressDialog.setTitle("Downloading"); 
      mProgressDialog.setMessage("Downloading sth..."); 
      mProgressDialog.setIndeterminate(false); 
      mProgressDialog.setMax(100); 
      mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      DownloadMapTask downloadFile = new DownloadMapTask(ItemDetails.this); 
      downloadFile.execute(web_location_url); 
      mProgressDialog.show(); 

下載異步任務(兩種方法):

@Override 
protected String doInBackground(String... urls) { 
    int count; 

    PATH=maps_loc+"/Android/data/test/maps/"; 

    try { 
     URL url = new URL(urls[0]); 
     HttpURLConnection connection2 = (HttpURLConnection) url.openConnection(); 
     connection2.setRequestMethod("GET"); 
     connection2.setDoOutput(true); 
     connection2.connect(); 
     int lenghtOfFile = connection2.getContentLength(); 

     File apkdir = new File(PATH); 
     apkdir.mkdirs(); 

     File newInstall = new File(PATH, name+".tmp"); 

     InputStream input = new BufferedInputStream(url.openStream()); 
     OutputStream output = new FileOutputStream(newInstall); 

     byte data[] = new byte[1024]; 

     long total = 0; 

     while ((count = input.read(data)) != -1 && running==true) { 
      total += count; 
      publishProgress((int) (total * 100/lenghtOfFile)); 
      output.write(data, 0, count); 
     } 
     output.flush(); 
     output.close(); 
     input.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 

} 

public void onProgressUpdate(Integer... args) { 

    ItemDetails.mProgressDialog.setProgress(args[0]); 

} 
+0

可以請你告訴我們一些代碼...? – 2013-02-14 11:13:23

+0

當然,在一分鐘內 – Waypoint 2013-02-14 11:14:47

+0

你也可以檢查你的日誌。它可能是gc或其他東西。 – 18446744073709551615 2013-02-14 11:15:55

回答

1

有些服務器將關閉連接客戶端是否有速度慢,下載需要很長時間,如果你的程序是這樣的話通過移動數據而不是Wi-Fi連接到互聯網。

您應該考慮在您的程序中下載簡歷以免每次從頭開始時支持

我不認爲有種下載內存,你需要清除。我有一個應用程序,可以輕鬆下載超過50MB而沒有問題。

此外,您可能會考慮獲取Wi-Fi和處理器的lock,以保持程序運行,直到下載完成。

編輯

在你的代碼,嘗試打印值lenghtOfFileint lenghtOfFile = connection2.getContentLength();後,以確保它是一樣的,你正在下載的實際文件大小。

下面是替代示例代碼,它支持我在項目中使用的resume。 (它只是說明的想法,你將需要修改代碼以您的需求)

HttpClient httpClient = new DefaultHttpClient(); 
HttpGet request = new HttpGet(new URI(fileURL))); 
HttpResponse response; 
InputStream is = null; 
FileOutputStream fos = null; 
try { 

    boolean continueDownloading = false; 
    String tmpFileName = fileName + "_tmp"; 
    outputFile = new File(downloadFolder, tmpFileName); 
    if (outputFile.exists()) { 
      localFileLength = outputFile.length(); 
      if (localFileLength > 0) { 
        continueDownloading = true; 
      } 

      if (continueDownloading) { 
        request.addHeader("Range", "bytes=" + localFileLength + "-"); 
      } 

      response = httpClient.execute(request); 

      long remoteFileLength = 0; 
      Header contentLengthHeader = response.getFirstHeader("Content-Length"); 
      if (contentLengthHeader != null) { 
        remoteFileLength = Integer.parseInt(contentLengthHeader.getValue()); 
      } 

      long downloaded = 0; 

      if (continueDownloading) { 
        downloaded = localFileLength; 
      } 

      long fullFileLength = downloaded + remoteFileLength; 

      fos = new FileOutputStream(outputFile, true); 

      is = response.getEntity().getContent(); 


      byte[] buffer = new byte[DOWNLOAD_BUFFER_SIZE]; 
      int len = 0; 
      while ((len = is.read(buffer)) != -1 && isDownloading) { 
        fos.write(buffer, 0, len); 
        downloaded += len;          
      } 

      fos.flush(); 

      boolean success = downloaded == fullFileLength; 
      if (success) { 
        outputFile.renameTo(new File(downloadFolder, fileName)); 
      } 

    } catch (Throwable ex) { 
      ex.printStackTrace();    
    } finally { 
     // clean up resources 
    } 
+0

我沒有提到它,我有這個問題,而在Wi-Fi上,它正常工作,順便說一句 – Waypoint 2013-02-14 11:20:21

+0

因此,請確保您有適當的'lock'來保證您的程序在下載過程中處於活動狀態 – iTech 2013-02-14 11:22:44

+0

同時檢查' lenghtOfFile'並確保它是正確的 – iTech 2013-02-14 11:25:44

0

嘗試使用,而不是手動下載下載管理器,也有很多優勢,使用它。

下面是它的一個示例:DownloadManager Example

,並採取看看單證:DownloadManager

+0

我的應用必須向後兼容Android 2.1+,所以我不能使用下載管理器:( – Waypoint 2013-02-14 11:23:46

+0

你可以使用它2.3+設備,並改進2.2和2.1的手動下載實現 – 2013-02-14 11:24:48