for() 루프를 사용하여 AsyncTask에서 여러 파일을 다운로드 중입니다. 아래 코드는 정상적으로 작동하지만 각 파일은 자체 진행률 표시 줄과 단일 진행률 표시 줄로 다운로드되므로 다운로드 한 모든 파일에 대해 하나의 진행률 표시 줄 만 있으면됩니다. 아래하나의 progressbar java/Android로 여러 파일 다운로드
// ProgressDialog for downloading images
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type:
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setTitle("In progress...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}
그리고는 ..
class DownloadFileFromURL extends AsyncTask<String, Integer, String> {
/**
* Before starting background thread Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try {
for (int i = 0; i < f_url.length; i++) {
URL url = new URL(f_url[i]);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(
url.openStream(), 8192);
System.out.println("Data::" + f_url[i]);
// Output stream to write file
OutputStream output = new FileOutputStream(
"/sdcard/Images/" + i + ".jpg");
byte data[] = new byte[1024];
long total = 0;
int zarab=20;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress((int) ((total * 100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
//cc++;
}
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(Integer... progress) {
// setting progress percentage
pDialog.setProgress(progress[0]);
}
/**
* After completing background task Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
// Displaying downloaded image into image view
// Reading image path from sdcard
//String imagePath = Environment.getExternalStorageDirectory()
// .toString() + "/downloaded.jpg";
// setting downloaded into image view
// my_image.setImageDrawable(Drawable.createFromPath(imagePath));
}
}
다운로드 파일에 대한 AsyncTask를 또는 파일의 번호에 대한 ProgressBar의 쇼와 업그레이드 lenghtOfFile 대신 의미 경우에도 대체하고 도움이 해결책이 될 것입니다. 도움이 될 것입니다.
당신은 사전에 알고
가짜 진행률 표시 줄 방법을 다운로드 할 필요가 얼마나 많은 파일, 당신은에
ProgressDialog
총량을 설정할 수 있습니다
안녕하세요, 귀하의 작업 코드를 공유 할 수 있습니까? 미리 감사드립니다. –
@MuhammadSufiyan 제 작업 코드는 다음과 같습니다. 답변, 여기에서 코드를 공유 할 수 있습니다. 가능한 경우 도와 드리겠습니다. 문제를 해결하십시오. –
감사합니다. 내 문제는 내 댓글의 같은 날에도 해결되었습니다 .. 문제가 해결되었음을 알리는 것도 좋습니다. –