我試圖更新一個 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']中解壓縮代碼(http://developer.android.com/reference/android/os/AsyncTask.html)。在'onPreExecute()'中將ProgressBar從'doInBackground()'更新爲'onProgressUpdate()'並關閉它'onPostExecute()'。 – user370305 2012-08-14 07:12:29
看來,你正在做onCreate方法中的所有工作。雖然你應該產生一個單獨的線程解壓縮。 – harism 2012-08-14 07:13:21
@ user370305讓我這樣做 – Adam 2012-08-14 07:16:36