Issue
How should I be using AsyncTask class coupled with a progress bar to perform the copying process of a file to another directory in the local context of the phone sdcard? I have seen a similar example [here][1], but I have no idea how to incorporate the differences/modify the context of the code to suit my context to make it work?
Solution
It would be something like
// Params are input and output files, progress in Long size of
// data transferred, Result is Boolean success.
public class MyTask extends AsyncTask<File,Long,Boolean> {
ProgressDialog progress;
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(ctx,"","Loading...",true);
}
@Override
protected Boolean doInBackground(File... files) {
copyFiles(files[0],files[1]);
return true;
}
@Override
protected void onPostExecute(Boolean success) {
progress.dismiss();
// Show dialog with result
}
@Override
protected void onProgressUpdate(Long... values) {
progress.setMessage("Transferred " + values[0] + " bytes");
}
}
Now, inside copyFiles you will have to call publishProgress() with size of data transferred, for example. Note that progress generic parameter is Long. You can use CountingInputStream wrapper from commons-io for that.
There are number of additional things top take care of, but in the nutshell that is it.
To start:
MyTask task = new MyTask();
task.execute(src,dest);
Answered By - Alex Gitelman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.