Большое спасибо, проблема решена. Вы - лучший человек. Бог благословит вас, а также извините за мой английский и очень недельное знание о Java. Вы делаете каждый шаг, чтобы понять меня и помочь мне. еще раз спасибо
ружаю несколько файлов в AsyncTask с помощью цикла for (). Приведенный ниже код работает нормально, но для каждого файла, загруженного со своей собственной индикаторной строкой, требуется только один индикатор выполнения для всех загруженных файлов.
// 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;
}
}
И ниже AsyncTask для загрузки файлов ..
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));
}
}
Или, если Progressbar показывает и обновляет в отношении Nos of Files, вместо lenghtOfFile, это также будет альтернативным и полезным решением. Любая помощь будет высоко оценена.