加载数据库 - 如何在进度条上显示进度?
我正在尝试使用进度条,它显示加载数据库的进度,该数据库位于本地资产文件夹中。我不知道这种方式是否可行,因为我使用的是之前从互联网下载文件的代码。
我认为我必须获取数据库的大小,但我不知道该怎么做。 conection.getContentLength() 似乎不起作用。连接可能不包含地址 assets/kneipen2.sqlite 吗?
这是在 onCreate() 中:
new LoadFilesTask().execute("assets/kneipen2.sqlite");
这是 AsyncTask 内部类:
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) {
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;
}
因此,没有显示进度,但最终数据库已正确加载。请帮我。
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) {
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过两种方式使用它:
任何一个
AsyncTask 的 preexecute() 和 postexecute() 方法,其中 preexecute() 只显示进度对话框,而 postexecute() 则关闭相同的内容。
或者
使用处理程序显示进度对话框并关闭相同的...
希望你明白我说的...
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...
这可能有帮助
This might be of help