我有用progressbar压缩文件的代码,压缩成功了,但是progressbar得到max的速度很快,而压缩的操作还在运行,我试了很多次不知道是什么问题。这是我的密码:
final String inputFolderPath = "/storage/emulated/0/Alarms/123";
final String outZipPath = "/storage/emulated/0/Alarms/test.zip";
final ProgressBar progressbar1 = findViewbyId(R.id.progressbar1);
final Handler mainHandler = new Handler(Looper.getMainLooper());
Thread zipping;
zipping = new Thread(new Runnable() {
@Override
public void run() {
try {
java.io.FileOutputStream fos = new java.io.FileOutputStream(outZipPath);
java.util.zip.ZipOutputStream zos = new java.util.zip.ZipOutputStream(fos);
java.io.File srcFile = new java.io.File(inputFolderPath);
java.io.File[] files = srcFile.listFiles();
Log.d("", "Zip directory: " + srcFile.getName());
int bytesRead;
long totalBytesRead = 0;
long totalBytesReadé = 0;
for (int i = 0; i < files.length; i++) {
byte[] buffer = new byte[4 * 1024];
java.io.FileInputStream fis = new java.io.FileInputStream(files[i]);
zos.putNextEntry(new java.util.zip.ZipEntry(files[i].getName()));
totalBytesRead += files[i].length();
while (!Thread.currentThread().isInterrupted()
&&(bytesRead = fis.read(buffer)) != -1) {
zos.write(buffer, 0, bytesRead);
progressbar1.setMax((int)totalBytesRead);
totalBytesReadé += bytesRead;
final int fin = (int)(totalBytesReadé);
mainHandler.post(new Runnable() {
@Override
public void run() {
progressbar1.setProgress(fin);
}
});
}
zos.closeEntry();
fis.close();
}
zos.close();
} catch (java.io.IOException ioe) {
Log.e("", ioe.getMessage());
}
}
});
zipping.start();
我希望有人能帮我解决这个问题,提前谢谢
暂无答案!
目前还没有任何答案,快来回答吧!