1

Ich muss eine große Datei auf meiner App herunterladen (fast 1 GB) und ich fragte mich, was ist der beste Weg, um das zu tun und einige libs mir zu helfen. Zuerst schaute ich die Android Asynchronous Http Library an, aber ich fand keine Beispiele, die zeigen, wie man den Fortschritt veröffentlicht oder den Download pausiert. Dann weiß ich nicht, ob ich diese lib verwenden soll oder nur ein Standart-http commons verwenden soll. Anderes Problem ist, sollte ich einen Service benutzen, um meine Akte herunterzuladen?Der beste Weg, um eine große Datei in Android herunterzuladen und anzuhalten, fortzusetzen?

+0

Ihre Frage ein wiederholt. Bitte beachten Sie diese Frage, es könnte helfen [Laden Sie eine Datei mit Android, und zeigen Sie den Fortschritt in einem ProgressDialog] (http://StackOverflow.com/Questions/3028306/Download-a-File-with-android-and- showing-the-progress-in-a-Fortschrittdialog? noredirect = 1 & lq = 1) – Sanju

Antwort

0

Sie sollten verwenden, ein Systemdienst, der lange laufende HTTP-Downloads verarbeiten kann.

0

können Sie diesen Code in AsyncTask verwenden mit Lebenslauf zum Download:

@SuppressLint("Wakelock") 
    @Override 
    protected String doInBackground(String... sUrl) { 
     // take CPU lock to prevent CPU from going off if the user 
     // presses the power button during download 
     PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
     PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, 
      getClass().getName()); 
     wl.acquire(); 

     try { 
      InputStream input = null; 
      OutputStream output = null; 
      HttpURLConnection connection = null; 
      try { 
       URL url = new URL(sUrl[0]); 
       connection = (HttpURLConnection) url.openConnection(); 
       File SDCardRoot = Environment.getExternalStorageDirectory();      
       File file = new File(SDCardRoot,FOLDER_PATH1+FOLDER_PATH2+ "/"+fileName); 
       int downloaded=0; 
       if(file.exists()){ 
        downloaded=(int) file.length(); 
        connection.setRequestProperty("Range", "bytes=" + (int) file.length() + "-"); 
       } 
       else{ 
        file.createNewFile(); 
       } 
       connection.setDoInput(true); 
       connection.setDoOutput(true); 
       connection.connect(); 

       // expect HTTP 200 OK, so we don't mistakenly save error report 
       // instead of the file 


       // this will be useful to display download percentage 
       // might be -1: server did not report the length 
       int fileLength = connection.getContentLength()+(int)file.length(); 
       // download the file 
       input = connection.getInputStream();  
       if(downloaded>0){ 
        output = new FileOutputStream(file,true); 
       } 
       else{ 
        output = new FileOutputStream(file); 
       } 
       byte data[] = new byte[1024]; 
       long total = downloaded; 
       int count; 
       mProgressDialog.setMax(fileLength/1024); 
       while ((count = input.read(data)) != -1) { 
        // allow canceling with back button 
        if (isCancelled()) 
         return null; 
        total += count; 
        // publishing the progress.... 
        if (fileLength > 0) // only if total length is known 
         publishProgress((int)total/1024); 
        output.write(data, 0, count); 
       } 
       output.flush(); 
       if (output != null) 
        output.close(); 
       if (input != null) 
        input.close(); 
       if (connection != null) 
        connection.disconnect(); 
       wl.release(); 
       return null; 
      } catch (Exception e) { 
       return e.toString(); 
      } 
     } 
     catch (Exception e) { 
      return e.toString(); 
     }   
    } 
Verwandte Themen