1

私のアプリ(大体1GB)に大きなファイルをダウンロードする必要があり、私はそれを行うための最良の方法といくつかのライブラリが私を助けてくれるのだろうと思っていました。まずAndroidの非同期Httpライブラリを見ましたが、進行状況を公開する方法や開始する方法、ダウンロードを一時停止する方法を示す例は見つかりませんでした。それから私はこのlibを使うべきか、あるいはスタンダードなhttpコモンを使うべきかはわかりません。他の問題は、私は私のファイルをダウンロードするサービスを使用する必要がありますか?アンドロイドで大きなファイルをダウンロードして一時停止するには、最善の方法はありますか?

+0

あなたの質問は1を繰り返しています。この質問を参考にしてください。 [Androidでファイルをダウンロードし、ProgressDialogで進行状況を表示する](http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-進捗状況の進行表示を表示します(noredirect = 1&lq = 1) – Sanju

答えて

0

長時間実行されるHTTPダウンロードを処理できるシステムサービスであるDownLoad Managerを使用する必要があります。

0

あなたは履歴書をダウンロードするにはasynctaskでこのコードを使用することができます。

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

それは状態をダウンロードし維持するためのお手伝いを致します&それはあなたのすべてのテストケースを処理するため、あなたはライブラリを使用する必要があります。

ライブラリの一部:

1] ThinDownloadManager

2] Android-Download-Manager-Pro

関連する問題