I'm using an executor service in my app to download a file from a URL. I want to add a horizontal progress bar to show the download progress, but I'm facing some errors. How can I add a progress bar in the following code, without using async task?
private class ExecutorServiceDownload implements Runnable {
private String url;
public ExecutorServiceDownload(String url) {
this.url = url;
}
@Override
public void run() {
dlFile(url);
}
private String dlFile(String surl) {
try {
// I added progressbar here and it didn't download anything
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
URL url = new URL(surl);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
return "Server returned HTTP " + connection.getResponseCode() + " " + connection.getResponseMessage();
input = connection.getInputStream();
String title = URLUtil.guessFileName(String.valueOf(url), null, null);
output = new FileOutputStream(Environment.getExternalStorageDirectory().toString() + "/test_files" + "/" + title);
int contentLength = connection.getContentLength();
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
} catch (Exception e) {
return e.toString();
}
return null;
}
}
Use this code to download a file with executor service:
in onCreate
:
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progressbar);
downloading(); // to start the download
Downloading file with executor service:
private void downloading() {
progressBar.setVisibility(View.VISIBLE);
ExecutorService executor = Executors.newSingleThreadScheduledExecutor();
Handler handler = new Handler(Looper.getMainLooper());
executor.execute(new Runnable() {
int count;
@Override
public void run() {
// Background work here
try {
// put your url here, this is an example
URL url = new URL("http://techslides.com/demos/sample-videos/small.mp4");
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
// download the file
InputStream input = conection.getInputStream();
// catalogfile is your destenition folder
OutputStream output = new FileOutputStream(catalogfile + "video.mp4");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress
publishProgress(Integer.valueOf("" + (int)((total * 100) / lenghtOfFile)));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
handler.post(new Runnable() {
@Override
public void run() {
// UI Thread work here
progressBar.setVisibility(View.GONE);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
update progressbar
private void publishProgress(Integer...progress) {
progressBar.setProgress(progress[0]);
}
and for the horizontal progress bar, use:
<ProgressBar
android:max="100"
style="?android:attr/progressBarStyleHorizontal"
android:visibility="invisible"
android:id="@+id/progressbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"