2014-12-05 92 views
0

我可以一次下载一个文件,但是当我尝试下载另一个文件时(如果第一个文件正在处理中),则下载无法启动。第二个文件下载仅在第一个文件完成时启动。我想一次下载两个或更多文件。无法下载第一个文件时第二个文件

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

只是关于你的海量'.replace(..)'链旁注。请参阅http://stackoverflow.com/questions/10786042/java-url-encoding – trylimits 2014-12-05 10:15:01

+0

请问您如何实际执行'AsyncTask'?您定位的是哪个Android版本? – trylimits 2014-12-05 10:20:15

+0

我在清单中使用了11作为最小值和21作为最大值 – 2014-12-05 10:52:23

回答

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); 
    } 
} 
+0

如果你能解释你的代码是什么,那将是非常好的。这样,更多的人将能够获得收益。 :) – jazzurro 2014-12-05 11:00:03

+1

你能告诉我什么是你的代码的好处?因为你没有解释发生了什么,我可以一次下载两个或更多的文件吗? – 2014-12-05 11:24:53

+0

我的代码是用于从服务器上下载mp4文件并存储在手机的外部存储器中 – 2014-12-05 11:37:42

相关问题