进度条更新进度 [英] Progress Bar Update Progress
问题描述
我想在文件加密时运行 ProgressBar
。
I would like to have a ProgressBar
running while the file is being encrypt.
我有以下代码,但是我如何知道或分解大小,所以当它完成它达到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.
我是Android的新手,所以我还有一些我还不知道的事情,明白了。
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
,只需使用 CountingOutputStream
。您可以对 CipherInputStream
,但使用 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.
如果你提前明白了明文/密文的大小,这当然有帮助,但是我猜想说话自己可以通过标准的<$ c $ 找到文件的大小c> 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屋!