我能够一次下载一个文件,但是当我尝试下载另一个文件时,如果第一个文件正在进行中,则下载不会启动。第二个文件下载仅在第一个文件完成时开始。我想一次下载两个或更多文件。
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());
}
}
}
答案 0 :(得分:0)
public class DownloadTask extends AsyncTask<Integer, Integer, Void>{
private NotificationHelper mNotificationHelper;
String fileName;
String filePath;
public DownloadTask(Context context){
mNotificationHelper = new NotificationHelper(context);
}
protected void onPreExecute(){
//Create the notification in the statusbar
mNotificationHelper.createNotification("" + _file_name + ".doc".toString());
}
@Override
protected Void doInBackground(Integer... integers) {
//This is where we would do the actual download stuff
//for now I'm just going to loop for 10 seconds
// publishing progress every second
try {
// connecting to url
URL u = new URL(_file_url);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
fileName = "" + _file_name + ".doc".toString();
// lenghtOfFile is used for calculating download progress
int lenghtOfFile = c.getContentLength();
// this is where the file will be seen after the download
File file=new File(rootDir+ "/Apostolic/", fileName);
String filePath=file.getPath();
FileOutputStream f = new FileOutputStream(new File(rootDir
+ "/Apostolic/", fileName));
// file input is from the url
InputStream in = c.getInputStream();
// here's the download code
byte[] buffer = new byte[1024];
int len1 = 0;
long total = 0;
while ((len1 = in.read(buffer)) > 0) {
total += len1; // total = total + len1
publishProgress((int) ((total * 100) / lenghtOfFile));
f.write(buffer, 0, len1);
}
f.close();
} catch (Exception e) {
//Toast.makeText(_context, "Network Problem", Toast.LENGTH_LONG).show();
Log.d("DP", ""+e);
}
for (int i=10;i<=100;i += 10)
{
try {
Thread.sleep(1000);
publishProgress(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
//This method runs on the UI thread, it receives progress updates
//from the background thread and publishes them to the status bar
mNotificationHelper.progressUpdate(progress[0]);
}
protected void onPostExecute(Void result) {
//The task is complete, tell the status bar about it
mNotificationHelper.completed(_file_name,filePath);
}
}