Reputation: 5
I am trying to download a file from my server in Android and updating the progress..
On Async method in background I am calling a method that will download the file to my SD card now I would like update the progress hence trying publishProgress("" + progressvalue); which I am not able access from this method. How do I access the publishProgress?
Here is what I am trying:
private class FileDownloader extends AsyncTask<String, String, Void> {
@Override
protected Void doInBackground(String... param) {
downloadFileToSdCard(param[0], "File" + counter + ".mp4");
return null;
}
protected void onProgressUpdate(String... values)
{
Log.d("ANDRO_ASYNC", values[0]);
}
}
downloadFileToSdCard method looks like this:
private void downloadImagesToSdCard(String downloadUrl, String fileName)]
{
FileOutputStream fos;
InputStream inputStream = null;
try {
URL url = new URL(downloadUrl);
String sdCard = Environment.getExternalStorageDirectory().toString();
File myDir = new File(sdCard, "AppDownload");
if (!myDir.exists()) {
myDir.mkdir();
}
String fname = fileName;
File file = new File(myDir, fname);
if (file.exists())
file.delete();
URLConnection ucon = url.openConnection();
final String contentLengthStr=ucon.getHeaderField("content-length");
int lenghtOfFile = Integer.valueOf(contentLengthStr);
HttpURLConnection httpConn = (HttpURLConnection) ucon;
httpConn.setRequestMethod("GET");
httpConn.connect();
inputStream = httpConn.getInputStream();
fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int bufferLength = 0;
long total = 0;
int progress = 0;
while ((bufferLength = inputStream.read(buffer)) > 0)
{
total += bufferLength;
int progress_temp = (int) total * 100 / lenghtOfFile;
publishProgress("" + progress_temp);
if (progress_temp % 10 == 0 && progress != progress_temp)
{
progress = progress_temp;
}
fos.write(buffer, 0, bufferLength);
int downloadedSize = Integer.valueOf(contentLengthStr);
}
inputStream.close();
fos.close();
Log.e("Value", "File Saved in sdcard..");
} catch (IOException io) {
inputStream = null;
fos = null;
io.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
Can somebody help me fix this? Thanks!
Upvotes: 0
Views: 573
Reputation: 9315
The first thing that hits me is to simply pass your AsyncTask to your sdcard method. AsyncTask.publishProgress is protected, so as long as your two classes are in the same package, you should be able to call publishProgress from the sdcard downloaded method.
A cleaner/prettier approach would be to define a callback interface such as ProgressUpdater, let the AsyncTask implement it, and have the sdcard method take a ProgressUpdater as a parameter. This interface would typically have a similar or even equal publishProgress method. This however decouples the AsyncTask itself from the rest of your program.
Upvotes: 0
Reputation: 4032
Triy this:
keep you function inside into AsyncTask Class
private class FileDownloader extends AsyncTask<String, String, Void> {
@Override
protected Void doInBackground(String... param) {
downloadFileToSdCard(param[0], "File" + counter + ".mp4");
return null;
}
protected void onProgressUpdate(String... values)
{
Log.d("ANDRO_ASYNC", values[0]);
}
private void downloadImagesToSdCard(String downloadUrl, String fileName)
{
//Publish Progress calling code
}
}
Upvotes: 1