2012-05-11 8 views
1

urlからオーディオファイルを取得していて、them.nowとマッピングされている字幕があります。ダウンロードボタンをクリックすると.mp3ファイル& .srtファイル同じプログレスバーを共有し、同じ進捗状況をすべて表示する必要があります。 そして、彼らはsdcardの2つの異なるディレクトリに保存する必要があります。私はプログレスバーを使って1つのファイルのダウンロードを実装しました。どんな助けもありがとう!アンドロイドはシングルクリックで2つのファイルをダウンロードし、単一のプログレスバーで進行状況を共有します

ありがとうございました。

アップデート:すべてのものは、この種の検索に残っているため回答...

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を設定すると、それらを次々とダウンロードするように設定することができます。プログレスバーのステータスこのため

を組み合わせる

は、私はあなたがいずれかのファイルのダウンロードを開始する前に、各ファイルのサイズを取得する必要があると推測しています。プログレスバーを2つのサイズの合計に設定して、ダウンロードしたときにダウンロードしたバイト数でプログレスバーを更新します。次のファイルをダウンロードする際には、現在のダウンロード値を継続する必要があります。

以下は、非常にの概略です。疑似コードです。ファイルをどのようにダウンロードしているのか、どのようにスレッド化しているのか分かりません(うまくいけば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

関連する問題