【问题标题】:ProgressBar in AsyncTask is showing values more 100AsyncTask 中的 ProgressBar 显示的值超过 100
【发布时间】:2015-11-06 06:36:14
【问题描述】:

我在我的 Android 应用程序中使用ProgressBar 并在提取数据库期间显示值,但ProgressBar 值超过 100 并显示到 122%

我试过设置,

if(progress<=100)
publishProgress(progress);

但这会导致长时间显示 100%。

谁能帮助进度计算响应文件大小提取

这是我的代码:

@Override
protected Integer doInBackground(Void... params) {
    byte[] buf = new byte[8192];
    String name = null;

    try {
        System.out.println("doInBackground");
        listener.onDoInBackground(context,true);
        name = "db_sqlite.7z";
        //InputStream of the sqlite file
        InputStream in = context.getResources().openRawResource(R.raw.db_sqlite);
        int fileSize = in.available();
        FileOutputStream out = context.openFileOutput("db.sqlite", 0);//Context.MODE_PRIVATE);
        try {

         /*  In contrast to other classes in org.tukaani.xz,
             LZMAInputStream doesn't do buffering internally
             and reads one byte at a time. BufferedInputStream
             gives a huge performance improvement here but even
             then it's slower than the other input streams from
             org.tukaani.xz.
            in = new BufferedInputStream(in);   */

            in = new XZInputStream(in);
            int size;
            int counter=0;
            while ((size = in.read(buf)) != -1) {
                //System.out.write(buf, 0, size);
                if(this.isCancelled())
                    break;
                out.write(buf, 0, size);
                counter++;
                progress = (int) (counter*100*1024/(double)fileSize);
                publishProgress(progress);
            }

    protected void onProgressUpdate(Integer... values) {
    //System.out.println("onProgressUpdate");
    super.onProgressUpdate(values);
    listener.onProgressUpdation(context, true, values);
    }

在活动中:

    @Override
    public void onProgressUpdation(Context context, Boolean isStarted, Integer... values) {
                              tv_you_can_change.setText(context.getResources().getString(R.string.extracting_database) + "  " + values[0] + "%");
    tv_you_can_change.setVisibility(View.VISIBLE);
    progressBar.setProgress(values[0]);
    //System.out.println("onProgressUpdation");
    }

【问题讨论】:

  • 在将值设置为进度条之前检查values[0] 的值。即,在.setProgress(values[0]) 之前
  • @govindpatel:问题出在进度 = (int) (counter*100*1024/(double)fileSize);

标签: android android-activity android-asynctask android-sqlite android-progressbar


【解决方案1】:

InputStream.available() 不返回总字节数,它返回一个可以不阻塞读取的估计字节数。

在这种情况下,没有真正好的方法来确定总大小。但是,由于它是一种资源,因此您在编译时就知道大小,并且在运行时不会改变。因此,只需将大小存储在 Number 资源中(如果必须,也可以对其进行硬编码)。

为了确定读取的字节数,保留 in.read() 返回的数字的总和 然后,进度就是 bytesRead/fileSize。

【讨论】:

    【解决方案2】:

    试试这个:

     @Override
        protected Integer doInBackground(Void... params) {
            byte[] buf = new byte[8192];
            String name = null;
    
            try {
                System.out.println("doInBackground");
                listener.onDoInBackground(context,true);
                name = "db_sqlite.7z";
            //InputStream of the sqlite file
    
    
     InputStream in = context.getResources().openRawResource(R.raw.db_sqlite);
        int fileSize = in.available();
        FileOutputStream out = context.openFileOutput("db.sqlite", 0);//Context.MODE_PRIVATE);
        try {
    
         /*  In contrast to other classes in org.tukaani.xz,
             LZMAInputStream doesn't do buffering internally
             and reads one byte at a time. BufferedInputStream
             gives a huge performance improvement here but even
             then it's slower than the other input streams from
             org.tukaani.xz.
            in = new BufferedInputStream(in);   */
    
            in = new XZInputStream(in);
            int size;
            int counter=0;
            while ((size = in.read(buf)) != -1) {
                    //System.out.write(buf, 0, size);
    
    
      if(this.isCancelled())
                    break;
                out.write(buf, 0, size);
                counter++;
    
                progress = (int) (counter/(double)fileSize*100);
                publishProgress(progress);
            }
    
        protected void onProgressUpdate(Integer... values) {
        //System.out.println("onProgressUpdate");
    
    
         super.onProgressUpdate(values);
            listener.onProgressUpdation(context, true, values);
            }
    

    【讨论】:

    • 试过这种方式它没有显示任何值,0% 仍然是提取结束。
    【解决方案3】:

    是的,我找到了方法!

    //Original file size i.e size after extraction
    float fileSize = 160088064;
    

    提取后硬编码文件大小...

    try {
                // In contrast to other classes in org.tukaani.xz,
                // LZMAInputStream doesn't do buffering internally
                // and reads one byte at a time. BufferedInputStream
                // gives a huge performance improvement here but even
                // then it's slower than the other input streams from
                // org.tukaani.xz.
                //in = new BufferedInputStream(in);
    
                in = new XZInputStream(in);
                int size;
                int counter=0;
                while ((size = in.read(buf)) != -1) {
                    if(this.isCancelled())
                        break;
                    out.write(buf, 0, size);
                    counter++;
                    //System.out.println((counter*8192)/fileSize);
                    progress = (int) (((counter*bufferSize)/fileSize)*100);
                        publishProgress(progress);
                }
    

    感谢大家帮助找到这个...

    【讨论】:

      猜你喜欢
      • 2016-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-27
      • 2021-12-16
      • 1970-01-01
      • 2015-05-19
      相关资源
      最近更新 更多