进度条更新进度 [英] Progress Bar Update Progress

查看:155
本文介绍了进度条更新进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个进度运行时文件将被加密。

I would like to have a ProgressBar running while the file is being encrypt.

我有以下的code,但我怎么能知道或分解的大小,这样,当它完成它达到100%?如,更新进度条%,而加密工作。

I have the following code, but how can I know or break down the size, so that when it is done it reaches 100%? As in, updating the progress bar % while the encryption is working.

我是新来的Andr​​oid,所以我有相当多的事情,我还是不知道,听懂了没有。

I'm new to Android, so I have quite a bit of things I still do not know, understand yet.

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

}

推荐答案

您知道纯文本的大小,所以如果你写入的 CipherOutputStream ,只需换一个以<一个href=\"http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/output/CountingOutputStream.html\"相对=nofollow> CountingOutputStream 。您可以为 CipherInputStream ,但与<一个href=\"http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/input/CountingInputStream.html\"相对=nofollow> CountingInputStream 当然。在这种情况下,你最好把 CountingInputStream CipherInputStream 正如你可能提前知道密文的大小,不纯文本。你可能不关心不过,由于明文和密文应该是大小几乎相同 - 用户不应该看到太大的区别。这两个类都可以在Apache公共I / O库中找到。

You know the size of the plain text, so if you are writing to the CipherOutputStream, simply wrap that one with a CountingOutputStream. You can do the same for the CipherInputStream, but with a CountingInputStream of course. In that case you are better off putting the CountingInputStream within the CipherInputStream as you probably know the ciphertext size in advance, not the plain text. You may not care though, as the plain text and cipher text should be almost identical in size - a user should not see much difference. Both classes can be found in the Apache commons I/O libraries.

当然,它帮助,如果你知道明文/密文提前大小,但我想这是不言而喻的。文件的大小可以发现<一href=\"http://stackoverflow.com/questions/7255592/get-file-directory-size-using-java-7-new-io\">through标准的java.nio 库或的旧Java 6的新I / O API 。最后,你应该显然不是先将其写入流流之前的所有字节到内存,使用例如4KiB块大小,而不是(使用 的ByteBuffer 如果可能的话)。

It of course helps if you know the size of the plaintext / ciphertext in advance, but I guess that speaks for itself. The size of files can be found through the standard java.nio libraries or the older Java 6 new I/O API. Finally, you should obviously not first stream all the bytes to memory before writing it to the streams, use e.g. 4KiB block sizes instead (using ByteBuffer if possible).

这篇关于进度条更新进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆