我希望在文件加密时运行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;
}
}
答案 0 :(得分:1)
您知道纯文本的大小,因此如果您正在写CipherOutputStream
,只需用CountingOutputStream
包裹它即可。您可以对CipherInputStream
执行相同操作,但当然使用CountingInputStream
。在这种情况下,最好将CountingInputStream
放在CipherInputStream
中,因为您可能事先知道密文大小,而不是纯文本。您可能不在乎,因为纯文本和密文的大小应该几乎相同 - 用户不应该看到太多差异。这两个类都可以在Apache commons I / O库中找到。
如果您事先知道明文/密文的大小,那当然会有所帮助,但我想这说明了一切。可以找到through the standard java.nio
libraries或the older Java 6 new I/O API文件的大小。最后,在将其写入流之前,显然不应首先将所有字节流式传输到内存中,例如使用改为4KiB块大小(如果可能,使用ByteBuffer
)。
答案 1 :(得分:1)
看一下AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html 它会完全按照你想要的 - 比尝试使用原始线程容易得多。