我正在创建一个应用程序,用于解压缩列表视图中的所选项目。我正在使用asynctask来解压缩zip文件,所以当它解压缩文件时会有一个progressDialog来显示解压缩它的进度。我成功地显示了progressDialog但是当解压缩完成后,progressDialog没有被解雇而且它被卡在那里。我试图在这里搜索与我的答案相关的stackoverflow,我甚至在我的应用程序上尝试它,但它们都不起作用。请帮助。
这是我到目前为止所做的: 解压缩。的java
public class Decompress extends AsyncTask<Void, Integer, Integer> {
private final static String TAG = "Decompress";
private String zipFile;
private String location;
ProgressDialog myProgressDialog;
Context ctx;
public Decompress(String zipFile, String location, Context ctx) {
super();
this.zipFile = zipFile;
this.location = location;
this.ctx = ctx;
dirChecker("");
}
@Override
protected void onPreExecute() {
super.onPreExecute();
myProgressDialog = new ProgressDialog(ctx);
myProgressDialog.setMessage("Please Wait... Unzipping");
myProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
myProgressDialog.setCancelable(false);
myProgressDialog.show();
}
@Override
protected Integer doInBackground(Void... params){
int count = 0;
try {
ZipFile zip = new ZipFile(zipFile);
myProgressDialog.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 {
FileOutputStream fout = new FileOutputStream(location +ze.getName());
byte[] buffer = new byte[8192];
int len;
while ((len = zin.read(buffer)) != -1) {
fout.write(buffer, 0, len);
count++;
publishProgress(count);// Here I am doing the update of my progress bar
}
fout.close();
zin.closeEntry();
}
}
zin.close();
} catch(Exception e) {
Log.e("Decompress", "unzip", e);
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
myProgressDialog.setProgress(progress[0]); //Since it's an inner class, Bar should be able to be called directly
}
protected void onPostExecute(Integer... result) {
Log.i(TAG, "Completed. Total size: "+result);
if(myProgressDialog != null && myProgressDialog.isShowing()){
myProgressDialog.dismiss();
}
}
private void dirChecker(String dir)
{
File f = new File(location + dir);
if(!f.isDirectory())
{
f.mkdirs();
}
} }
这就是我在我的活动中称之为的方式 Archives.java
String zipFile = o.getPath();
String unzipLocation = Environment.getExternalStorageDirectory() + filePath;
Decompress d = new Decompress(zipFile, unzipLocation, Archive.this);
d.execute();
非常感谢你。