I have a problem with Progress Bar showing % of download completed. I have a service class in which the downloading takes place. I can get the start time and end time of download from this class. In Main Activity on clicking a button I have to show the progress %. I heard that its possible through AsyncTask but I have no clue about how it works. Please help me with some sample code example related to this. Thanks
Asked
Active
Viewed 2.5k times
2
-
1See this example, it also provides **CODE**, http://samir-mangroliya.blogspot.in/p/android-asynctask-example.html Just download it and learn new things. – Chintan Raghwani Jul 16 '12 at 12:00
-
1Look at http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog – user370305 Jul 16 '12 at 12:00
-
hi please look at this it might be helpful to u http://stackoverflow.com/questions/7712370/updating-progressbar-from-intentservice and may this also be helpful http://toolongdidntread.com/android/android-multipart-post-with-progress-bar/ – Jay Thakkar Jul 16 '12 at 12:17
3 Answers
4
I would prefer AsyncTask for this.
Here's an example
ProgressDialog mProgressDialog;
// instantiate it within the onCreate method
mProgressDialog = new ProgressDialog(YourActivity.this);
mProgressDialog.setMessage("A message");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// execute this when the downloader must be fired
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute("the url to the file you want to download");
here's the AsyncTask
private class DownloadFile extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
try {
URL url = new URL(sUrl[0]);
URLConnection connection = url.openConnection();
connection.connect();
// this will be useful so that you can show a typical 0-100% progress bar
int fileLength = connection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/file_name.extension");
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
}
return null;
}
This can be done using Download from Service as well as Download Manager class. Refer this question for details.
EDIT
The percentage completed is what you actually publish in the progress dialog. If you want to display the percentage, you may use this (total * 100 / fileLength).
int percentage = (total * 100 / fileLength);
TextView tv = (TextView)findViewById(R.id.textview);
tv.setText("" + percentage);
Use this code to display the percentage in the desired textview.
-
1hi, there is only a progress dialog which is displayed. The progress is not displayed. I did the same thing like how you said. – Lavanya Jul 25 '12 at 06:57
-
this will give u the percentage (total * 100 / fileLength). you may accept the answer if it helped. – darsh Jul 25 '12 at 08:34
0
try like this
here is a class
public class XYZ extends Activity {
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private Button startBtn;
private ProgressDialog mProgressDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startBtn = (Button)findViewById(R.id.startBtn);
startBtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
startDownload();
}
});
}
private void startDownload() {
String url = "http://.jpg";
new DownloadFileAsync().execute(url);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading file..");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
class DownloadFileAsync extends AsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/some_photo_from_gdansk_poland.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
} }
and here is a xml file
Best of luck Aamirkhan I.

Aamirkhan
- 5,746
- 10
- 47
- 74
-
1hi, thanks for the reply. The progress is still not shown. I did the same thing like how you specified. – Lavanya Jul 25 '12 at 07:51
0
Progress bar with counting %
//you can use these method after Firebase Storage upload task
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading...");
progressDialog.show();
progressDialog.setCanceledOnTouchOutside(false);
StorageReference reference = storageReference
.child("images/" + UUID.randomUUID().toString());
reference.putFile(filePath).addOnProgressListener(new
OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(@NonNull UploadTask.TaskSnapshot
taskSnapshot) {
double progress =
(100.0*taskSnapshot.getBytesTransferred()/taskSnapshot.getTotalByteCount());
progressDialog.setMessage("Uploaded "+
(int)progress+"%");
}
});
Result

Anshul Tikariha
- 24
- 2