AsyncTask 中的 ProgressBar 显示的值大于 100

ProgressBar in AsyncTask is showing values more 100

我在 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);
    }

在 activity:

    @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");
    }

InputStream.available() 不是 return 总字节数,它是 return 可以无阻塞读取的估计字节数。

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

为了确定读取的字节数,保留 in.read() 编辑的 return 总数 运行 那么,进度就是bytesRead/fileSize.

试试这个:

 @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);
        }

是的,我找到路了!

//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);
            }

感谢大家帮助发现这个...