2011-04-02 102 views
3

我正在嘗試製作一個應用程序,可以幫助我評估從Web資源下載文件的時間。我發現2個樣品:更新進度對話框

Download a file with Android, and showing the progress in a ProgressDialog

http://www.helloandroid.com/tutorials/how-download-fileimage-url-your-device

第二個例子顯示了一個更小的下載時間,但我不知道如何使用它來更新進度對話框。我認爲應該用第二種情況下的「while」表達來完成,但我找不到。有人能給我任何建議嗎?

UPD:

第一代碼:

try { 
      time1 = System.currentTimeMillis(); 
      URL url = new URL(path); 
      URLConnection conexion = url.openConnection(); 
      conexion.connect(); 
      // this will be useful so that you can show a tipical 0-100% progress bar 
      int lenghtOfFile = conexion.getContentLength(); 
      // downlod the file 
      InputStream input = new BufferedInputStream(url.openStream()); 
      OutputStream output = new FileOutputStream("/sdcard/analyzer/test.jpg"); 

      byte data[] = new byte[1024]; 

      long total = 0; 

      time11 = System.currentTimeMillis(); 
      while ((count = input.read(data)) != -1) { 
       total += count; 
       // publishing the progress.... 
       publishProgress((int)(total*100/lenghtOfFile)); 
       output.write(data, 0, count); 
      } 
      time22= System.currentTimeMillis()-time11; 
      output.flush(); 
      output.close(); 
      input.close(); 


     } catch (Exception e) {} 

     timetaken = System.currentTimeMillis() - time1; 

第二碼:

 long time1 = System.currentTimeMillis(); 
     DownloadFromUrl(path, "test.jpg"); 
     long timetaken = System.currentTimeMillis() - time1; 

public void DownloadFromUrl(String imageURL, String fileName) { //this is the downloader method 
try { 
     URL url = new URL(imageURL); //you can write here any link 
     File file = new File(fileName); 

     /*Open a connection to that URL. */ 
     URLConnection ucon = url.openConnection(); 

     /* 
      * Define InputStreams to read from the URLConnection. 
      */ 
     InputStream is = ucon.getInputStream(); 
     BufferedInputStream bis = new BufferedInputStream(is); 

     /* 
      * Read bytes to the Buffer until there is nothing more to read(-1). 
      */ 
     ByteArrayBuffer baf = new ByteArrayBuffer(50); 
     int current = 0; 
     while ((current = bis.read()) != -1) { 
       baf.append((byte) current); 
     } 

     /* Convert the Bytes read to a String. */ 
     FileOutputStream fos = new FileOutputStream(PATH+file); 
     fos.write(baf.toByteArray()); 
     fos.close(); 

} catch (IOException e) { 
     Log.d("ImageManager", "Error: " + e); 
} 

所以事情是,第一種方法似乎是慢約30%。

回答

2

second example可能運行得更快,但它獨佔了GUI線程。 first approach,使用AsyncTask更好;它允許GUI在下載過程中保持響應。

我發現將AsyncTaskSwingWorker相比較很有幫助,如此example所示。

+0

我在Asynctask中執行第二種方法。 – StalkerRus 2011-04-02 18:31:15

+0

那麼,第一個代碼有一個1024字節的緩衝區,而第二個只有50個。 – trashgod 2011-04-02 18:38:00

+0

我試圖減少到50的緩衝區。但我沒有看到任何性能的變化。 – StalkerRus 2011-04-02 18:53:29

1

第一個環節是最好的。但我無法提供代碼(這是家庭比較)在星期一或以後我可以提供完整的功能。但是:

private class DownloadFile extends AsyncTask<String, Integer, String>{ 
    @Override 
    protected String doInBackground(String... url) { 
     int count; 
     try { 
      URL url = new URL(url[0]); 
      URLConnection conexion = url.openConnection(); 
      conexion.connect(); 
      // this will be useful so that you can show a tipical 0-100% progress bar 
      int lenghtOfFile = conexion.getContentLength(); 

      // downlod the file 
      InputStream input = new BufferedInputStream(url.openStream()); 
      OutputStream output = new FileOutputStream("/sdcard/somewhere/nameofthefile.ext"); 

      byte data[] = new byte[1024]; 

      long total = 0; 

      while ((count = input.read(data)) != -1) { 
       total += count; 
       // publishing the progress.... 
       publishProgress((int)(total*100/lenghtOfFile)); 
       output.write(data, 0, count); 
      } 

      output.flush(); 
      output.close(); 
      input.close(); 
     } catch (Exception e) {} 
     return null; 
    } 

這個類最適合它(imho)。 publishProgress它是一個簡單的函數,其中最多有兩行。設置最大和設置電流。如何在這段代碼中看到lenghtOfFile這是ur文件有多少個字節。 total - 當前進度(示例25從100字節)。輕鬆運行這門課程:DownloadFile a = new DownloadFile(); a.execute(value,value);//or null if u not using value.希望你能理解我,我不會說英語。

+0

我明白它是如何工作的,但我發現使用第一個代碼我下載的文件比使用第二個代碼慢大約30%。我不明白爲什麼。 – StalkerRus 2011-04-02 18:21:09

+0

代碼引用:http://stackoverflow.com/questions/3028660 – trashgod 2011-04-02 18:21:48

+0

字節數據[] =新字節[1024];你需要在這裏設置新的值,例如16000。它的incresa下載速度 – Peter 2011-04-02 20:23:36