I am able to Download one file at a time, but when I try to download another file if first one is in progress then download does not starts. The Second file download start only when first one is finished. I want to download two or more files at a time.
private class Downloader extends AsyncTask<Void, Integer, Integer> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Displays the progress bar for the first time.
mBuilder.setProgress(100, 4, false);
mNotifyManager.notify(id, mBuilder.build());
}
@Override
protected void onProgressUpdate(Integer... values) {
// Update progress
mBuilder.setProgress(100, values[0], false);
mNotifyManager.notify(id, mBuilder.build());
super.onProgressUpdate(values);
}
@Override
protected Integer doInBackground(Void... params) {
try {
String streamingUrl = new String(retVidUrl.trim()
.replace(" ", "%20").replace("&", "%26")
.replace(",", "%2c").replace("(", "%28")
.replace(")", "%29").replace("!", "%21")
.replace("=", "%3D").replace("<", "%3C")
.replace(">", "%3E").replace("#", "%23")
.replace("$", "%24").replace("'", "%27")
.replace("*", "%2A").replace("-", "%2D")
.replace(".", "%2E").replace("/", "%2F")
.replace(":", "%3A").replace(";", "%3B")
.replace("?", "%3F").replace("@", "%40")
.replace("[", "%5B").replace("\\", "%5C")
.replace("]", "%5D").replace("_", "%5F")
.replace("`", "%60").replace("{", "%7B")
.replace("|", "%7C").replace("}", "%7D"));
String fStream = Uri.decode(streamingUrl);
if (fStream != null) {
System.out
.print("This is the final url from wherer you can download"
+ fStream);
;
URL u = null;
InputStream is = null;
try {
u = new URL(fStream);
is = u.openStream();
HttpURLConnection huc = (HttpURLConnection) u
.openConnection();// to know the size of video
size = huc.getContentLength();
System.out
.println("Seeeeeeeeeeeeeeeeeeeeeeee here >>>>>>"
+ size);
if (huc != null) {
String mp44 = "mp4";
String fileName = size + "." + mp44;
// String fileName = "videoplayback.mp4";
// String storagePath =
// Environment.getExternalStorageDirectory().toString();
String storagePath = Environment
.getExternalStorageDirectory().toString()
+ "/" + "Download";
File f = new File(storagePath, fileName);
fos = new FileOutputStream(f);
byte[] buffer = new byte[1024];
int len1 = 0;
Long downloadedSize = (long) 0;
if (is != null) {
while ((len1 = is.read(buffer)) > 0) {
downloadedSize += len1;
publishProgress((int) (downloadedSize * 100 / size));
fos.write(buffer, 0, len1);
}
}
if (fos != null) {
fos.close();
}
}
} catch (Exception e) {
Log.e("Error Msg", e.toString());
}
}
} catch (Exception e) {
Log.e("Error Msg", e.toString());
}
}
}