0

我正在开发一个文件加密和解密应用程序,并向用户显示文件加密和解密的一点进度条。我需要实现一个进度条。我怎么做 ?对于下面的代码。

    public void main(String[] args) throws Exception
      {

         // File to decrypt.
      filename = "/file.enc";

      String password = "codecodecode";

      inFile = new FileInputStream(new File(Environment.getExternalStorageDirectory()+ filename));
      outFile = new FileOutputStream(new File(Environment.getExternalStorageDirectory()+ filename + ".txt"));

      PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
      SecretKeyFactory keyFactory =
          SecretKeyFactory.getInstance("PBEWithMD5AndDES");
      SecretKey passwordKey = keyFactory.generateSecret(keySpec);

      // Read in the previouly stored salt and set the iteration count.

      byte[] salt = new byte[8];
      inFile.read(salt);
      int iterations = 100;

      PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, iterations);

      // Create the cipher and initialize it for decryption.

      Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
      cipher.init(Cipher.DECRYPT_MODE, passwordKey, parameterSpec);


      byte[] input = new byte[64];
      int bytesRead;
      while ((bytesRead = inFile.read(input)) != -1)
      {
         byte[] output = cipher.update(input, 0, bytesRead);
         if (output != null)
            outFile.write(output);
      }

      byte[] output = cipher.doFinal();
      if (output != null)
         outFile.write(output);

      inFile.close();
      outFile.flush();
      outFile.close();
  }
4

1 回答 1

0

查看进度条教程:https ://developer.android.com/guide/topics/ui/dialogs.html#ShowingAProgressBar

还有“带有第二个线程的示例 ProgressDialog” - 很高兴检查。

还可以考虑AsyncTask用于此类事情,它内置支持在 UI 线程上发布进度更新事件。

于 2012-05-16T09:37:20.377 回答