2013-03-19 255 views
1

我想在文件加密時運行ProgressBar進度條更新進度

我有以下代碼,但是如何知道或分解大小,以便在完成時達到100%?正如在更新加密工作時的進度欄%。

我是新來的Android,所以我有一些我仍然不知道,理解的東西。

import java.io.File; 

public class ProgressBarExa extends Activity { 

Button btnStartProgress; 
ProgressDialog progressBar; 
private int progressBarStatus = 0; 
private Handler progressBarHandler = new Handler(); 

// private long fileSize = 0; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.progressbar_view); 

    addListenerOnButton(); 

} 

public void addListenerOnButton() { 

    btnStartProgress = (Button) findViewById(R.id.btnStartProgress); 
    btnStartProgress.setOnClickListener(
      new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

     // prepare for a progress bar dialog 
     progressBar = new ProgressDialog(v.getContext()); 
     progressBar.setCancelable(true); 
     progressBar.setMessage("File encrypting..."); 
     progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     progressBar.setProgress(0); 
     progressBar.setMax(100); 
     progressBar.show(); 

     //reset progress bar status 
     progressBarStatus = 0; 

     //reset filesize 
     // fileSize = 0; 

     new Thread(new Runnable() { 
      public void run() { 
      while (progressBarStatus < 100) { 

       // process some tasks 
       progressBarStatus = doSomeTasks(); 

       // your computer is too fast, sleep 1 second 
       try { 
       Thread.sleep(1000); 
       } catch (InterruptedException e) { 
       e.printStackTrace(); 
       } 

       // Update the progress bar 
       progressBarHandler.post(new Runnable() { 
       public void run() { 
        progressBar.setProgress(progressBarStatus); 
       } 
       }); 
      } 

      // ok, file is downloaded, 
      if (progressBarStatus >= 100) { 

       // sleep 2 seconds, so that you can see the 100% 
       try { 
        Thread.sleep(2000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 

       // close the progress bar dialog 
       progressBar.dismiss(); 
      } 
      } 
      }).start(); 

      } 

      }); 

    } 

// file download simulator... a really simple 
public int doSomeTasks() { 

    try{ 
     String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath(); 
     String fileName = "a.wmv"; 
     String newFileNEE = "b.wmv"; 
     String newFileNED = "c.wmv"; 

     FileInputStream fis = new FileInputStream(new File(baseDir + File.separator + fileName)); 

     File outfile = new File(baseDir + File.separator + newFileNEE); 
      int read; 
      if(!outfile.exists()) 
       outfile.createNewFile(); 

      // long outfile_size = outfile.length(); 

      File decfile = new File(baseDir + File.separator + newFileNED); 
      if(!decfile.exists()) 
       decfile.createNewFile(); 


      FileOutputStream fos = new FileOutputStream(outfile); 
      FileInputStream encfis = new FileInputStream(outfile); 
      FileOutputStream decfos = new FileOutputStream(decfile); 

      Cipher encipher = Cipher.getInstance("AES"); 
      Cipher decipher = Cipher.getInstance("AES"); 

      KeyGenerator kgen = KeyGenerator.getInstance("AES"); 
      SecretKey skey = kgen.generateKey(); 
      encipher.init(Cipher.ENCRYPT_MODE, skey); 
      CipherInputStream cis = new CipherInputStream(fis, encipher); 
      decipher.init(Cipher.DECRYPT_MODE, skey); 
      CipherOutputStream cos = new CipherOutputStream(decfos,decipher); 

      while((read = cis.read())!=-1) 
        { 
         fos.write((char)read); 
         fos.flush(); 
        } 
      fos.close(); 
      while((read=encfis.read())!=-1) 
      { 
       cos.write(read); 
       cos.flush(); 
      } 
      cos.close(); 

    }catch (Exception e) { 
     // TODO: handle exceptione 
     e.printStackTrace(); 
    } 
    return 100; 
} 

}

回答

1

你知道純文本的大小,所以,如果你正在寫的CipherOutputStream,只是換一個帶有CountingOutputStream。您可以對CipherInputStream執行相同操作,但當然也可以執行CountingInputStream。在這種情況下,您最好將CountingInputStream置於CipherInputStream之內,因爲您可能事先知道密文大小,而不是純文本。你可能不在乎,因爲純文本和密文的大小几乎相同 - 用戶不應該看到太大的差別。這兩個類都可以在Apache commons I/O庫中找到。

當然,如果你事先知道明文/密文的大小,當然會有所幫助,但我想這是說明問題。文件的大小可以找到through the standard java.nio librariesthe older Java 6 new I/O API。最後,在寫入流之前,你顯然不應該首先將所有的字節流到內存中,例如,改爲4KiB塊大小(如果可能,使用ByteBuffer)。

+0

謝謝,我想我會去嘗試一下AysncTask。 – user1778855 2013-03-20 13:44:03