2012-05-11 30 views
1

我得到一些音頻文件從URL和他們的字幕與他們映射。現在我想下載.mp3文件& .srt文件點擊下載按鈕,他們也應該共享相同的進度條並顯示相同的進度。 他們應該保存在兩個不同的SD卡目錄中。我已經用進度條實現了單個文件下載。任何幫助,將不勝感激!android下載單個點擊兩個文件,並在單個進度條中共享他們的進度

謝謝。

更新:答案爲那些仍然在尋找這樣的......

String strGetFName; 
    String strVipEpub; 

    public DownloadFile() 
    { 
     strGetFName = getFilename(); 
     strVipEpub = getVipLessonEpubFilename(); 

    } 

    @Override 
    protected String doInBackground(String... sUrl) 
    { 
     try 
     { 
      URL url = new URL(sUrl[0]); 
      URL urlePub = new URL(sUrl[1]); 
      Log.e("urls"," "+sUrl[0]+","+sUrl[1]); 
      URLConnection connection = url.openConnection(); 
      URLConnection connectionePub = urlePub.openConnection(); 
      Log.e("connection","done"); 
      connection.connect(); 
      connectionePub.connect(); 
      Log.e("connection","Connected"); 
      // this will be useful so that you can show a typical 0-100% progress bar 
      int fileLength = connection.getContentLength(); 
      int fileLengthePub = connectionePub.getContentLength(); 
      Log.e("fileLength"," "+fileLength); 
      int bytesFromMP3 = fileLength; 
      int bytesFromSubtitle = fileLengthePub; 
      int totalBytesToDownload = bytesFromMP3 + bytesFromSubtitle; 
      Log.e("bytesFromMP3"," "+bytesFromMP3); 
      Log.e("bytesFromSubtitle"," "+bytesFromSubtitle); 
      Log.e("total"," "+totalBytesToDownload); 

      int bytesDownloadedSoFar = 0; 
      while(bytesDownloadedSoFar <= bytesFromSubtitle) 
      { 
       // download the file 
       InputStream input = new BufferedInputStream(urlePub.openStream()); 

       OutputStream output = new FileOutputStream(strVipEpub); 

       byte data[] = new byte[1024]; 
       long total = 0; 
       int count; 
       while ((count = input.read(data)) != -1) 
       { 
        total += count; 
        bytesDownloadedSoFar += count; 
        // publishing the progress.... 
        publishProgress((int) (bytesDownloadedSoFar * 100/totalBytesToDownload)); 
        output.write(data, 0, count); 
       } 

       output.flush(); 
       output.close(); 
       input.close(); 
      } 
      while(bytesDownloadedSoFar <= bytesFromMP3) 
      { 
       // download the file 
       InputStream input = new BufferedInputStream(url.openStream()); 

       OutputStream output = new FileOutputStream(strGetFName); 

       byte data[] = new byte[1024]; 
       long total = 0; 
       int count; 
       while ((count = input.read(data)) != -1) 
       { 
        total += count; 
        bytesDownloadedSoFar += count; 
        // publishing the progress.... 
        publishProgress((int) (bytesDownloadedSoFar * 100/totalBytesToDownload)); 
        output.write(data, 0, count); 
       } 

       output.flush(); 
       output.close(); 
       input.close(); 
      } 
     } 
     catch (Exception e) 
     { 
      Log.e("File"," Not Downloaded"+e.getMessage()); 
     } 
     return null; 
    } 
    @Override 
    protected void onPreExecute() 
    { 
     super.onPreExecute(); 
    } 

    @Override 
    protected void onProgressUpdate(Integer... progress) 
    { 
     super.onProgressUpdate(progress); 
     pbDownloading.setProgress(progress[0]); 
     tvProgress.setText(progress[0].toString()+"%"); 
    } 

    @Override 
    protected void onPostExecute(String result) 
    { 
     super.onPostExecute(result); 
     Log.e("is ","Downloading complete"); 
    } 
+0

發佈您的當前代碼。 – FoamyGuy

回答

4

下載多個文件

如果你有單一的下載工作,它應該是那樣簡單複製代碼並將其更改爲下載.srt文件。如果您設置了AsyncTask,您可以將它們設置爲一個接一個下載。

相結合的進度欄狀態

對於這一點,我猜,你開始下載的任何文件之前,你需要得到每個文件的大小。將進度條設置爲兩種尺寸的總和,然後在下載時使用下載的字節數更新進度條。當您下載下一個文件時,當前的下載值將不得不繼續。

下面是一些非常粗糙的僞代碼。我不知道你是如何下載文件以及如何對它進行線程化的(希望它像AsyncTask)。以下是我將要應用的邏輯。如果您向我們展示您的代碼,我們可以更具體。

int bytesFromMP3 = GetSizeOfFileAtURL(urlOfMP3File); 
int bytesFromSubtitle = GetSizeOfFileAtURL(urlOfSubtitlesFile); 
int totalBytesToDownload = bytesFromMP3 + bytesFromSubtitle; 

progressBar.setMax(totalBytesToDownload); 

int bytesDownloadedSoFar = 0; 

//Start some loop to download the MP3 File 
while(...) 
{ 
    //Download some data from the MP3 File 
    ... 

    //Update the number of bytes downloaded based on the size of the chunk you just downloaded 
    bytesDownloadedSoFar += sizeOfChunkJustDownloaded 

    //Update the progress bar 
    progressBar.setProgress(bytesDownloadedSoFar); 
} 

//Repeat the loop, but this time download the subtitle info. Don't reset bytesDownloadedSoFar, just continue to add to it. 
while(...) 
+0

感謝您的迴應和基本理念。將嘗試它,並得到回你..... – skygeek

+0

現在正在工作,我已經實現了你的想法,它成爲我的工作......謝謝 – skygeek

+0

偉大的,你可以請將這個問題標記爲答案?謝謝。 – Gophermofur