2011-10-12 96 views
0

我正在製作一個應用程序,我需要從服務器下載文件並將其保存到android設備的SD卡中。Android暫停並恢復將文件下載到SD卡

除此之外,還有一個用戶可以暫停和恢復文件的功能。

我現在面臨的問題是,我能夠下載文件到SD卡,但是當嘗試暫停和恢復它,它就會開始從一開始.......

這裏是源。 .....

public class DownloadWithProgressActivity extends Activity { 

    public static final int DIALOG_DOWNLOAD_PROGRESS = 0; 
    private Button startBtn, pauseButton, resumeButton; 
    private ProgressBar bar; 
    URLConnection conexion; 
    OutputStream output; 
    static int count = 0; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     startBtn = (Button) findViewById(R.id.startBtn); 
     pauseButton = (Button) findViewById(R.id.button1); 
     resumeButton = (Button) findViewById(R.id.button2); 
     bar = (ProgressBar) findViewById(R.id.progressBar1); 
     bar.setVisibility(ProgressBar.INVISIBLE); 
     bar.setId(0); 

     startBtn.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       startDownload(); 
      } 
     }); 
     pauseButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       try { 
        output.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     }); 
     resumeButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // not getting what to write here 
      } 
     }); 
    } 

    private void startDownload() { 
     String url = "http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg"; 
     new DownloadFileAsync().execute(url); 
    } 

    class DownloadFileAsync extends AsyncTask<String, String, String> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      bar.setVisibility(ProgressBar.VISIBLE); 

     } 

     @Override 
     protected String doInBackground(String... aurl) { 
      int downloaded = 0; 
      File file = new File("/sdcard/some_photo_from_gdansk_poland.jpg"); 
      URL url = null; 
      try { 
       url = new URL(aurl[0]); 
      } catch (MalformedURLException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      try { 
       conexion = url.openConnection(); 
       if (file.exists()) { 
        downloaded = (int) file.length(); 
        conexion.setRequestProperty("Range", "bytes="+(file.length())+"-"); 

       } 

       else { 
        conexion.setRequestProperty("Range", "bytes=" + downloaded 
          + "-"); 
       } 
      } catch (Exception exception1) { 
      } 
      conexion.setDoInput(true); 
      conexion.setDoOutput(true); 

      System.out.println(tryGetFileSize(url)); 
      conexion.setRequestProperty("Range", "bytes=" + count + "-"); 
      try { 
       conexion.connect(); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      int lenghtOfFile = conexion.getContentLength(); 
      Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); 
      InputStream input = null; 
      try { 
       input = new BufferedInputStream(url.openStream()); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      try { 
       output = new FileOutputStream(file); 
      } catch (FileNotFoundException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      byte data[] = new byte[1024]; 
      long total = 0; 
      try { 
       while ((count = input.read(data)) != -1) { 
        total += count; 
        publishProgress("" + (int) ((total * 100)/lenghtOfFile)); 
        output.write(data, 0, count); 
       } 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      try { 
       output.flush(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      try { 
       output.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      try { 
       input.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      return null; 

     } 

     protected void onProgressUpdate(String... progress) { 
      Log.d("ANDRO_ASYNC", progress[0]); 
      bar.setProgress(Integer.parseInt(progress[0])); 
      System.out.println(Integer.parseInt(progress[0])); 
     } 

     protected void onPostExecute(String unused) { 
     } 

     int tryGetFileSize(URL url) { 
      HttpURLConnection conn = null; 
      try { 
       conn = (HttpURLConnection) url.openConnection(); 
       conn.setRequestMethod("HEAD"); 
       conn.getInputStream(); 
       return conn.getContentLength(); 
      } catch (IOException e) { 
       return -1; 
      } finally { 
       conn.disconnect(); 
      } 
     } 

    } 
} 

請幫忙.......

感謝 尼基爾

+0

沒有'seek to()'方法進行下載(不確定) –

回答

2
connection.setRequestProperty("Range", "bytes=" + ***downloaded*** + "-"); 

使"downloaded"成爲全局變量,因爲下載從這個位置開始。

當您按下重新啓動按鈕時,檢查此下載變量的值,絕不能全部爲零。

0

此行返回一個錯誤的文件大小,我用同樣的代碼來打開的淨Zip文件:不是通過關閉輸出

int lenghtOfFile = conexion.getContentLength(); 

我怎樣可以暫停下載其他?

pauseButton.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      try { 
       output.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    }); 
resumeButton.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 
     // not getting what to write here 
    } 
});