1

내 응용 프로그램 (거의 1GB)에 큰 파일을 다운로드해야하고 그 일을하는 가장 좋은 방법은 무엇인지, 일부 라이브러리는 나를 도와 주는지 궁금합니다. 우선 Android Asynchronous Http 라이브러리를 살펴 보았지만 진행 상황을 게시하거나 다운로드를 일시 중지하는 방법을 보여주는 예제는 찾지 못했습니다. 그렇다면이 lib를 사용해야하는지 또는 표준 http 커먼을 사용해야하는지 모릅니다. 다른 문제는, 내 파일을 다운로드하는 서비스를 사용해야합니까 ??안드로이드에서 큰 파일을 다운로드하고 일시 중지하는 가장 좋은 방법은 무엇입니까?

+0

귀하의 질문을 반복한다. 이 질문을 참조하십시오. 도움이 될 것 같습니다. [Android로 파일을 다운로드하고 ProgressDialog에서 진행 상황을 표시합니다.] (http://stackoverflow.com/questions/3028306/download-a-file-with-android-and- progress-in-the-progressdialog = 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(); 
     }   
    } 
관련 문제