2011-06-01 59 views
1

我有一個應用程序正在下載一個zip文件,然後將此文件複製到手機上sd卡上的臨時文件,但速度非常慢。Android將大輸入流複製到文件很慢

 InputStream in = new BufferedInputStream(url.openStream(), 1024); 
     File tempFile = File.createTempFile("arc", ".zip", targetDir); //target dir is a file 
     String tempFilePath = tempFile.getAbsolutePath(); 
     OutputStream out = new BufferedOutputStream(new FileOutputStream(tempFile)); 

//copying file (in different void)  
     byte[] buffer = new byte[8192]; 
     int len; 
     len = in.read(buffer); 
enter code here 

//it loops here for AGES 
     while (len >= 0) {  
      out.write(buffer, 0, len); 
      len = in.read(buffer); 
       } 
      in.close(); 
      out.close(); 

我的文件是20MB左右,最初我有1024的緩衝區大小,並把它改爲8192想着它可以加快速度,但它似乎沒有什麼區別?我總是完成,而且我沒有得到任何錯誤,只需要很長時間!

我已經搜索嘗試找到一個解決方案,但我沒有提出任何事情,所以我可能會採取完全錯誤的方式?

任何人都可以看到我做錯了什麼?

Bex

+0

檢查它不是你的互聯網連接。怎麼樣?如果你評論「寫」行,並沒有什麼區別......它需要很長的時間來閱讀......所以它必須是連接。 – helios 2011-06-01 13:10:23

+0

我的代碼沒有看到任何問題,所以我會說它是I/O綁定的。也許服務器或SD卡速度很慢?你是否嘗試過閱讀而不寫或寫虛擬數據,看看哪部分是瓶頸? – Waldheinz 2011-06-01 13:10:46

+0

啊所以它不會下載實際的文件,直到流被讀取? – Bex 2011-06-01 13:25:21

回答

0

Donot增加緩衝區大小。這可能會導致您的應用程序MemoryOutOfBoundsException。

有各種因素,您的下載速度會很慢。弱互聯網連接,弱文件傳輸和接收模式也是負責任的。它也取決於設備的容量。檢查您是否使用下面的代碼創建的InputStream

URL u = new URL("enter url url here"); 
       HttpURLConnection c = (HttpURLConnection) u.openConnection(); 
       c.setRequestMethod("GET"); 
       c.setDoOutput(true); 
       c.connect(); 
       InputStream in = c.getInputStream(); 

感謝 迪帕克

+0

這是互聯網連接! – Bex 2011-06-06 09:17:34