2012-02-11 62 views
2

我使用下面的代碼從互聯網下載文件。使用此代碼我有一個進度對話框文件傳輸到SD的進度條

private void startDownload() { 
     String url = tv.getText().toString(); 
     new DownloadFileAsync().execute(url); 
    } 

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

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      showDialog(DIALOG_DOWNLOAD_PROGRESS); 
     } 

     @Override 
     protected String doInBackground(String... aurl) { 
      int count; 

      try { 

       URL url = new URL(aurl[0]); 
       URLConnection conexion = url.openConnection(); 
       conexion.connect(); 

       int lenghtOfFile = conexion.getContentLength(); 
       Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); 

       InputStream input = new BufferedInputStream(url.openStream()); 
       OutputStream output = new FileOutputStream(
         "data/data/com.xxxxxxxx.android/app_p_pic/" 
           + "blabla" 
       byte data[] = new byte[1024]; 

       long total = 0; 

       while ((count = input.read(data)) != -1) { 
        total += count; 
        publishProgress("" + (int) ((total * 100)/lenghtOfFile)); 
        output.write(data, 0, count); 
       } 

       output.flush(); 
       output.close(); 
       input.close(); 
      } catch (Exception e) { 

       e.printStackTrace(); 

      } 
      return null; 

     } 

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

     @Override 
     protected void onPostExecute(String unused) { 
      dismissDialog(DIALOG_DOWNLOAD_PROGRESS); 
     } 


    } 
} 

是有使用上面的代碼傳送從文件的方式說內部SD?在momment我用下面的代碼,但IV一直無法得到進度與進度條工作

protected void ontrans() { 
     String title = tv.getText().toString(); 

     try { 

      myInput = new FileInputStream(title); 


      File directory = new File(Environment.getExternalStorageDirectory() 
        + "/android_files/data/secure"); 
      if (!directory.exists()) { 
       directory.mkdirs(); 
      } 

      OutputStream myOutput = new FileOutputStream(
        Environment.getExternalStorageDirectory() 
          + "/android_files/data/secure/" + chosenFile); 

      byte[] buffer = new byte[1024]; 
      int length; 
      while ((length = myInput.read(buffer)) > 0) { 
       myOutput.write(buffer, 0, length); 
      } 

      myOutput.flush(); 

      myOutput.close(); 

      myInput.close(); 
      Toast.makeText(File_exActivity.this, "file move Succesfully!", 
        Toast.LENGTH_SHORT).show(); 
      showDialog(11); 
     } catch (FileNotFoundException e) { 
      Toast.makeText(File_exActivity.this, "no file found!", 
        Toast.LENGTH_LONG).show(); 

      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      Toast.makeText(File_exActivity.this, "move unsuccesfull!", 
        Toast.LENGTH_LONG).show(); 

      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

回答

0

需要考慮的事情:您可以獲得源文件File.length()和目標的大小。比較它們以顯示進度欄中的差異。

File。長度()方法以下根據javadoc的返回:

長度,以字節爲單位,此抽象路徑名, 或0L表示如果該文件不存在的文件的。某些操作系統可能會返回0L 作爲表示系統相關實體(如設備或 管道)的路徑名。

0

什麼你需要做的是創建一個進度條對象(你可以將其添加到您的佈局如果你想要或以編程方式創建它),然後在你的下載while循環內更​​新它的狀態。創建進度條的方法可以在這裏找到:http://developer.android.com/reference/android/widget/ProgressBar.html

+0

我已經得到了一個進度條,在第一個代碼中工作,代碼從互聯網下載,第二個代碼我用來從SD轉移到內部,到SDD ..我需要的是第一個代碼能夠在顯示進度的同時轉移文件或顯示進度的第二個代碼。 – steo 2012-02-12 09:43:53