【发布时间】:2012-08-14 07:09:13
【问题描述】:
我正在尝试更新
progress bar 解压 SD 卡中的文件。我的解压缩工作正常,但 progress bar 没有出现。这是我在 mainactivity 中的代码:
private ProgressBar bar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bar = (ProgressBar) findViewById(R.id.progress);
String zipFilename = Environment.getExternalStorageDirectory() + "path to my zip file in sd card";
String unzipLocation = Environment.getExternalStorageDirectory() + "the output folder";
Decompress d = new Decompress(zipFilename, unzipLocation);
d.unzip();
}
public class Decompress {
private String _zipFile;
private String _location;
private int per = 0;
public Decompress(String zipFile, String location) {
_zipFile = zipFile;
_location = location;
_dirChecker("");
}
public void unzip() {
try {
ZipFile zip = new ZipFile(_zipFile);
bar.setMax(zip.size());
FileInputStream fin = new FileInputStream(_zipFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
Log.v("Decompress", "Unzipping " + ze.getName());
if(ze.isDirectory()) {
_dirChecker(ze.getName());
} else {
// Here I am doing the update of my progress bar
per++;
bar.setProgress(per);
FileOutputStream fout = new FileOutputStream(_location + ze.getName());
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
} catch(Exception e) {
Log.e("Decompress", "unzip", e);
}
}
private void _dirChecker(String dir) {
File f = new File(_location + dir);
if(!f.isDirectory()) {
f.mkdirs();
}
}
}
}
【问题讨论】:
-
一切都好,现在把你的解压缩码输入
AsyncTask。在onPreExecute()中启动ProgressBar 将其从doInBackground()更新为onProgressUpdate()并关闭它onPostExecute()。 -
您似乎在 onCreate 方法中完成了所有工作。虽然你应该生成一个单独的线程来解压缩。
-
@AdamKhan - den,它会为你工作..
标签: android progress-bar zipfile