我正在尝试使用ProgressBar,它显示加载数据库的进度,该数据库位于本地资产文件夹中。我不知道这是甚至可能的,因为我使用之前从互联网上下载文件的代码。加载数据库 - 如何在progressBar中显示进度?
我认为我必须得到数据库的大小,但我不知道如何做到这一点。 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;
}
因此,有未示出的进步,但在最后的数据库正确加载。请帮帮我。
是的,我得到你说的,谢谢。它工作得很好。所以,这个解决方案是一个很好的解决方案,它可以工作,但最后,如果可能的话,我会出于布局的原因,仍然想让progressBar显示加载本地文件的进度。那可能吗?如果没有,有没有办法对progressDialog的外观产生影响? – 10ff