扩展文件下载示例未显示完整进度

时间:2013-11-07 12:10:37

标签: android progress-bar apk-expansion-files

目前,我正准备将扩展文件纳入已开发项目的演示。 我已按照此Tutorial

经过大量的努力,我能够运行演示应用程序,但在显示下载数据的进度方面存在问题。

我在播放商店上传了obb文件 1,060,024字节,磁盘大小 1,060,864字节

以下是我认为可能存在问题的代码段。

private static final XAPKFile[] xAPKS = { new XAPKFile(true,6,1060024L) };


void validateXAPKZipFiles() {
        AsyncTask<Object, DownloadProgressInfo, Boolean> validationTask = new AsyncTask<Object, DownloadProgressInfo, Boolean>() {

            @Override
            protected void onPreExecute() {
                mDashboard.setVisibility(View.VISIBLE);
                mCellMessage.setVisibility(View.GONE);
                mStatusText.setText(R.string.text_verifying_download);
                mPauseButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mCancelValidation = true;
                    }
                });
                mPauseButton.setText(R.string.text_button_cancel_verify);
                super.onPreExecute();
            }

            @Override
            protected Boolean doInBackground(Object... params) {
                for (XAPKFile xf : xAPKS) {
                    String fileName = Helpers.getExpansionAPKFileName(
                            MainActivity.this, xf.mIsMain, xf.mFileVersion);
                    if (!Helpers.doesFileExist(MainActivity.this, fileName,
                            xf.mFileSize, true)) {
                        return false;
                    }
                    fileName = Helpers.generateSaveFileName(MainActivity.this,
                            fileName);
                    ZipResourceFile zrf;
                    byte[] buf = new byte[1024 * 256];
                    try {
                        zrf = new ZipResourceFile(fileName);
                        ZipEntryRO[] entries = zrf.getAllEntries();
                        /**
                         * First calculate the total compressed length
                         */
                        long totalCompressedLength = 0;
                        for (ZipEntryRO entry : entries) {
                            totalCompressedLength += entry.mCompressedLength;
                        }
                        float averageVerifySpeed = 0;
                        long totalBytesRemaining = totalCompressedLength;
                        long timeRemaining;
                        /**
                         * Then calculate a CRC for every file in the Zip file,
                         * comparing it to what is stored in the Zip directory.
                         * Note that for compressed Zip files we must extract
                         * the contents to do this comparison.
                         */
                        for (ZipEntryRO entry : entries) {
                            if (-1 != entry.mCRC32) {
                                long length = entry.mUncompressedLength;
                                CRC32 crc = new CRC32();
                                DataInputStream dis = null;
                                try {
                                    dis = new DataInputStream(
                                            zrf.getInputStream(entry.mFileName));

                                    long startTime = SystemClock.uptimeMillis();
                                    while (length > 0) {
                                        int seek = (int) (length > buf.length ? buf.length
                                                : length);
                                        dis.readFully(buf, 0, seek);
                                        crc.update(buf, 0, seek);
                                        length -= seek;
                                        long currentTime = SystemClock
                                                .uptimeMillis();
                                        long timePassed = currentTime
                                                - startTime;
                                        if (timePassed > 0) {
                                            float currentSpeedSample = (float) seek
                                                    / (float) timePassed;
                                            if (0 != averageVerifySpeed) {
                                                averageVerifySpeed = SMOOTHING_FACTOR
                                                        * currentSpeedSample
                                                        + (1 - SMOOTHING_FACTOR)
                                                        * averageVerifySpeed;
                                            } else {
                                                averageVerifySpeed = currentSpeedSample;
                                            }
                                            totalBytesRemaining -= seek;
                                            timeRemaining = (long) (totalBytesRemaining / averageVerifySpeed);
                                            Log.e("Remaining Size is :: ",
                                                    " "
                                                            + (totalCompressedLength - totalBytesRemaining));
                                            this.publishProgress(new DownloadProgressInfo(
                                                    totalCompressedLength,
                                                    totalCompressedLength
                                                            - totalBytesRemaining,
                                                    timeRemaining,
                                                    averageVerifySpeed));
                                        }
                                        startTime = currentTime;
                                        if (mCancelValidation)
                                            return true;
                                    }
                                    if (crc.getValue() != entry.mCRC32) {
                                        Log.e(Constants.TAG,
                                                "CRC does not match for entry: "
                                                        + entry.mFileName);
                                        Log.e(Constants.TAG, "In file: "
                                                + entry.getZipFileName());
                                        return false;
                                    }
                                } finally {
                                    if (null != dis) {
                                        dis.close();
                                    }
                                }
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                        return false;
                    }
                }
                return true;
            }

            @Override
            protected void onProgressUpdate(DownloadProgressInfo... values) {
                onDownloadProgress(values[0]);
                super.onProgressUpdate(values);
            }

            @Override
            protected void onPostExecute(Boolean result) {
                if (result) {
                    mDashboard.setVisibility(View.VISIBLE);
                    mCellMessage.setVisibility(View.GONE);
                    mStatusText.setText(R.string.text_validation_complete);
                    mPauseButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            finish();
                        }
                    });
                    mPauseButton.setText(android.R.string.ok);
                } else {
                    mDashboard.setVisibility(View.VISIBLE);
                    mCellMessage.setVisibility(View.GONE);
                    mStatusText.setText(R.string.text_validation_failed);
                    mPauseButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            finish();
                        }
                    });
                    mPauseButton.setText(android.R.string.cancel);
                }
                super.onPostExecute(result);
            }

        };
        validationTask.execute(new Object());
    }

我没有使用补丁文件,只使用主文件。

每次下载数据时,都显示随机进度,如下Screenshot 1 Screen Shot 2

实际上它下载完整数据但无法验证完整数据。以下是打印剩余字节时的logcat条目以进行验证。

  

11-08 12:22:12.205:E /剩余尺寸为::( 20326):46804 11-08   12:22:13.370:E /剩余尺寸为::( 20326):99054 11-08   12:22:13.460:E /剩余尺寸为::( 20326):203827 11-08   12:22:14.035:E /剩余尺寸为::( 20326):465971 11-08   12:22:14.115:E /剩余尺寸为::( 20326):728115 11-08   12:22:14.155:E /剩余尺寸为::( 20326):813899 11-08   12:22:14.270:E /剩余尺寸为::( 20326):850970 11-08   12:22:14.295:E /剩余尺寸为::( 20326):868781 11-08   12:22:14.320:E /剩余尺寸为::( 20326):1041595

我无法确定问题,如果任何机构面临这个问题会有很大的帮助。

提前致谢。

1 个答案:

答案 0 :(得分:1)

这里的问题是,总大小是根据压缩文件中的条目计算的,这导致压缩文件中文件的未压缩大小。并且正在以相同的方式展示进展。

  1. 第一个解决方案是,为了检查扩展文件的可用性,请将检查保留在应用程序中,但计算时请提供原始大小。 正在对zip文件库中的压缩条目进行计算,如果可能,相应地进行修改。 (我没有尝试过这个解决方案。)

  2. 第二个是,压缩文件的方式是文件夹内的文件是未压缩的。在这种情况下,您将无所事事,但缺点是用户必须下载更大尺寸的文件。