Reputation: 1259
I am getting a list of files from a web service and then using AsyncTask to handle the download of all files. Each task is created in a loop:
for(int i = 0; i < arraySize; i++) {
//Omitted all code except for the portion that fires the asynctask
//Download the PDF
DownloadHandler dhpdf = new DownloadHandler(cleanLink, fileName, p1, DownloadPosters.this, "pdf");
dhpdf.startDownload();
//Download the PNG
DownloadHandler dhpng = new DownloadHandler(cleanLink, fileName, p1, DownloadPosters.this, "png");
dhpng.startDownload();
}
The Download class
public class DownloadHandler extends Activity {
private Context mContext;
//File ======================
public String filename;
private String remotePath;
private File file;
private String ext;
//Progress bar ==============
private ProgressBar progressBar;
private int progressStatus = 0;
//private Handler handler = new Handler();
private TextView mTextView;
//ProgressDialog ============
ProgressDialog mProgress;
private int mProgressDialog=0;
public DownloadHandler(String rp, String f, ProgressBar progressBar, Context c, String extension, TextView textview) throws Exception {
mContext = c;
remotePath = rp;
filename = f;
file = new File(mContext.getFilesDir(), filename+"."+extension);
ext = extension;
this.progressBar = progressBar;
mTextView = textview;
}
//our method
public void startDownload() {
String url = "http://examplesite.com/"+remotePath+"/"+filename+"."+ext;
new DownloadFileAsync().execute(url);
}
class DownloadFileAsync extends AsyncTask<String, Integer, String> {
@Override
public void onPreExecute() {
}
@Override
protected void onProgressUpdate(Integer... values) {
progressBar.setProgress(values[0]);
mTextView.setText("Downloading: "+ext);
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection conection = url.openConnection();
conection.connect();
// Get Music file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(),10*1024);
// Output stream to write file in internal storage
OutputStream output = new BufferedOutputStream(new FileOutputStream(file));
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// Publish the progress which triggers onProgressUpdate method
publishProgress((int) ((total * 100) / lenghtOfFile));
// Write data to file
output.write(data, 0, count);
}
// Flush output
output.flush();
// Close streams
output.close();
input.close();
} catch (Exception e) {mTextView.setText("ERROR:"+e.toString());}
return null;
}
@Override
protected void onPostExecute(String unused) {
mTextView.setText("Complete");
}
}
Right now I am only able to test about 6 files and it seems to be working well.
My question is if this is the proper way to que up multiple downloads and can this handle 100+ files at a time without crashing?
Upvotes: 0
Views: 248
Reputation: 5893
Why not using an Android Download Manager ? You can queue all of your download requests, and it's a service, it will work in the background. It also checks your connection, resumes downloads when the connection is gone and re established.
For more information and a quick start, check this tutorial. It should help. Vogella Blog
Upvotes: 2
Reputation: 75644
No 100 should not be a problem, but you should also not be doing 100 paralel uploads - that's not going to work too well (technically it shall work but in terms of performance it's wrong approach). I'd queue them and push one by one.
Please note that depending on Android version, AsyncTask
will work differently when called in default "form" (new xxTask.execute()`) - on older version if will go parallel, on never it will go one by one. Ensure you do this always the same.
Finally, if you really know you going to have 100 uploads fired, then I'd sit down and rework your app's architecture - you definitely got some things very wrong there.
Upvotes: 1