在android中实现进度条

时间:2012-05-16 09:32:28

标签: android file progress-bar

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

    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();
  }

1 个答案:

答案 0 :(得分:0)

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

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

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