2013-02-14 75 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