EDIT: If I do not use the progress at all, just showing the dialog, then everything works without any problems. But if i add progress, then dialog doesn't close(method onPostExecute() is not fired).
onPostExecute method is not executed. Where do I have a mistake? (Same result on emulator and on the device) I am also not sure if I should use Override notation for these methods
This is the rough solution for now. It works every time, but is not the proper and not the nice one. What I am doing: * I start a child activity using tabgroupactivity * Then i navigate to another activity in the child activity, so it is a current child of a parent * There is a webview, where I display information about one comment.. whatever * There is a link in the content displayed by the webView * when i click it, i start downloading the PDF file.
When the file is downloaded:
while ((current = bis.read()) != -1) {
read = read + current;
baf.append((byte) current);
Dialog.setProgress(read);
if(Dialog.isShowing() && read+2*current>file_size){
Dialog.dismiss();
Dialog.cancel();
}
}
My Dialog object is disapeared, so if I try to call Dialog after the While loop, i just don't get it. So what I did is that every time i get new buffer from website, i check if the dialog is still visible and if the current byte together with the amount of bytes read up to now are greater than the full size of the file, then I close the dialog in the while loop. I tried using fileSize == read(amount of bytes), but it did not work, maybe they are not precisely mach each other when the file is downloaded
private class DownloadPDFFile extends AsyncTask<String, Integer, Void> {
private ProgressDialog Dialog = new ProgressDialog(getParent());
protected void onPreExecute() {
Dialog.setMessage("Downloading PDF file..");
Dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
Dialog.setCancelable(false);
// Dialog.setMax(1000);
Dialog.setProgress(0);
Dialog.show();
}
protected Void doInBackground(String... urls) {
File file=null;
int file_size = 0;
try {
URL url = new URL(urls[0]);
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
file_size = urlConnection.getContentLength();
Dialog.setMax(file_size);
} catch (IOException e) {
}
try {
URL url1 = new URL(urls[0]); // you can // link
file = new File("skm_intern_pdf.pdf");
URLConnection ucon = url1.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(5000);
int read = 0;
int current = 0;
while ((current = bis.read()) != -1) {
read = read + current;
baf.append((byte) current);
Dialog.setProgress(read);
if(Dialog.isShowing() && read+2*current>file_size){
Dialog.dismiss();
Dialog.cancel();
}
}
FileOutputStream fos = openFileOutput("skm_pdf.pdf",
Context.MODE_WORLD_WRITEABLE);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
} catch (IOException e) {
}
return null;
}