Load Database - how to show progress in progressBar?
I'm trying to use a ProgressBar, which shows the progress of loading a database, which is in the local assets folder. I don't know if it's even possible this way, because I'm using code which was before for downloading a file from the internet.
I think that I would have to get the size of the database, but I have no idea how to do this. The conection.getContentLength() doesn't seem to work. May the conection doesn't contain the address assets/kneipen2.sqlite ?
This is in the onCreate():
new LoadFilesTask().execute("assets/kneipen2.sqlite");
And this is the AsyncTask inner class:
private class LoadFilesTask extends AsyncTask<String, Integer, Integer> {
@Override
protected Integer doInBackground(String... urls) {
//oeffneDatenbank();
int count;
try {
myDbHelper = new DataBaseHelper(Start.this);
try {
myDbHelper.createDataBase();
}
catch (IOException ioe) { 开发者_如何学JAVA
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
URL url = new URL(urls[0]);
//Ignore this! Only used to show that operations can take a long time to complete...
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
// downlod File
InputStream input = new BufferedInputStream(url.openStream());
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int)(total*100/lenghtOfFile));
}
input.close();
} catch (Exception e) {}
return null;
}
So, there isn't shown a progress, but at the end the database is loaded correctly. Please help me.
You can use it in two ways : Either preExecute() and postExecute() method of AsyncTask where in preExecute() just show thr progressDialog and in postExecute() dismiss the same. or Use Handler to show progress dialog and dismiss the same...
Hope u get what i say...
ProgressDialog MyDialog = ProgressDialog.show( YourCurrentClass.this, "Please wait!" , "Loading..", true);
This might be of help
精彩评论