HELP: ProgressDialog.dismiss
Salve a tutti, ho scritto la seguente app che non fa altro che copiare un ebook su una data cartella dello smartphone. Creo una ProgressDialog per visualizzare lo stato di avanzamento ma alla fine l'istruzione dismiss non la chiude.... la progress rimane visualizzata su schermo. Dove sto sbagliando?
Grazie in anticipo!
package blade.books;
import java.io.ByteArrayInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.res.AssetManager;
import android.content.res.Resources.Theme;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ArrayAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;
public class BladeBook extends Activity
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
setContentView(R.layout.main);
InputStream book = getResources().openRawResource(R.raw.dissidenti);
final class DownloadFile extends AsyncTask<InputStream, Integer, Long>
{
ProgressDialog mProgressDialog;
@Override
protected void onPreExecute()
{
mProgressDialog = new ProgressDialog(BladeBook.this);
mProgressDialog.setMessage("Downloading the book...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.ST YLE_HORIZONTAL);
mProgressDialog.show();
}
protected Long doInBackground(InputStream... book)
{
int c;
long total = 0;
try
{
File sdcardDir = Environment.getExternalStorageDirectory();
File file = new File(sdcardDir,"/BladeBooks");
file.mkdirs();
file = new File(sdcardDir,"/BladeBooks/Dissidenti.epub");
if (file.exists()==false)
{
FileOutputStream fos = new FileOutputStream(file);
DataOutputStream dos = new DataOutputStream(fos);
int lenghtOfFile = 437000;
while ((c=book[0].read())!=-1)
{
total += c;
publishProgress((int)(total*100/lenghtOfFile));
dos.write(c);
}
dos.close();
}
}
catch (IOException e)
{
//output.setText(output.getText()+"-errore:"+e.getMessage());
}
return total;
}
@Override
public void onProgressUpdate(Integer... progress)
{
// here you will have to update the progressbar
// with something like
mProgressDialog.setProgress(progress[0]);
}
@Override
public void onPostExecute(Long result)
{
mProgressDialog.dismiss();
}
}
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute(book);
}
private int checkExternalMedia()
{
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
int nret=0;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state))
{
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
}
else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state) )
{
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
nret=1;
}
else
{
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
//Log.i(TAG,"State="+state+" Not good");
mExternalStorageAvailable = mExternalStorageWriteable = false;
nret=2;
}
//Log.i(TAG,"Available="+mExternalStorageAvailable+" Writeable="+mExternalStorageWriteable+" State"+state);
//return (mExternalStorageAvailable && mExternalStorageWriteable);
return(nret);
}
}