我正在嘗試使用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