加载数据库 - 如何在进度条上显示进度?

发布于 2024-12-06 13:49:36 字数 1852 浏览 0 评论 0原文

我正在尝试使用进度条,它显示加载数据库的进度,该数据库位于本地资产文件夹中。我不知道这种方式是否可行,因为我使用的是之前从互联网下载文件的代码。

我认为我必须获取数据库的大小,但我不知道该怎么做。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小嗲 2024-12-13 13:49:36

您可以通过两种方式使用它:
任何一个
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...

轮廓§ 2024-12-13 13:49:36
ProgressDialog MyDialog = ProgressDialog.show( YourCurrentClass.this, "Please wait!" , "Loading..", true);

这可能有帮助

ProgressDialog MyDialog = ProgressDialog.show( YourCurrentClass.this, "Please wait!" , "Loading..", true);

This might be of help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文