我从 url 获取一些音频文件及其映射的字幕。现在我想通过单击下载按钮下载该 .mp3 文件和 .srt 文件,并且它们应该共享相同的进度条并显示相同的总进度. 他们应该保存在sdcard的两个不同目录中。我已经实现了带有进度条的单个文件下载。任何帮助,将不胜感激!
谢谢。
更新:所有那些仍在寻找这种类型的人的答案......
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");
}