【问题标题】:Android: How do I obtain an app's total size dynamically?Android:如何动态获取应用程序的总大小?
【发布时间】:2012-07-31 11:58:13
【问题描述】:

在 Android 中,我看到许多带有加载屏幕的应用程序。我知道 ProgressBar,实现了 AsyncTask 方法,但我从未见过有人知道他们如何设法获得应用程序总大小的值,或者如果他们正在加载资产,则总资产大小。

通过动态获取应用程序的总大小,我将能够在发布时或发布后向应用程序添加额外的内容。然后我就可以计算应用加载的总进度。

到目前为止,我做了一个解决方法,如下所示:

package nttu.edu.activities;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.Queue;

import nttu.edu.R;
import nttu.edu.graphics.Art;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ProgressBar;

public class NewLoadingActivity extends Activity {
    public ProgressBar bar;
    private AssetManager assetManager;
    public Handler handler;
    public ProgressTask task;

    private final String[] list = {
    // Art.sprites
    "art/sprites.png", ...... };

    private class ProgressTask extends AsyncTask<Void, Void, Void> {
        public int totalByteSize;
        public int currentByteSize;
        public Queue<Bitmap> bitmapQueue;
        public Queue<byte[]> byteQueue;

        public ProgressTask() {
            totalByteSize = 0;
            currentByteSize = 0;
            bitmapQueue = new LinkedList<Bitmap>();
            byteQueue = new LinkedList<byte[]>();
        }

        public void onPostExecute(Void params) {
            Art.sprites = bitmapQueue.remove();
            finish();
        }

        public void onPreExecute() {
            try {
                for (int i = 0; i < list.length; i++) {
                    byte[] bytes = readFromStream(list[i]);
                    totalByteSize += bytes.length;
                    byteQueue.add(bytes);
                }
                bar.setMax(totalByteSize);
            }
            catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        public void onProgressUpdate(Void... params) {
            bar.setProgress(currentByteSize);
        }

        @Override
        protected Void doInBackground(Void... params) {
            while (currentByteSize < totalByteSize) {
                try {
                    Thread.sleep(1000);
                    if (byteQueue.size() > 0) {
                        byte[] bytes = byteQueue.remove();
                        Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                        bitmapQueue.add(bitmap);
                        currentByteSize += bytes.length;
                        this.publishProgress();
                    }
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        private byte[] readFromStream(String path) throws IOException {
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length = 0;
            InputStream input = assetManager.open(path);
            while (input.available() > 0 && (length = input.read(buffer)) != -1)
                output.write(buffer, 0, length);
            return output.toByteArray();
        }

    }

    public void onCreate(Bundle b) {
        super.onCreate(b);
        this.setContentView(R.layout.progressbar);
        assetManager = this.getAssets();
        handler = new Handler();
        task = new ProgressTask();
        bar = (ProgressBar) this.findViewById(R.id.loadingBar);
        if (bar == null) throw new RuntimeException("Failed to load the progress bar.");
        task.execute();
    }

    public void finish() {
        Intent intent = new Intent(this, MenuActivity.class);
        intent.putExtra("Success Flag", Art.sprites != null);
        this.setResult(RESULT_OK, intent);
        super.finish();
    }
}

此活动的唯一主要问题是我必须首先将新资产添加到靠近顶部的列表中。列表数组大小为 58。:

private final String[] list = {"art/sprites.png", ......};

然后从我要加载资产的列表中找出总资产大小,然后使用总资产大小在我的 ProgressBar 上标记 100%,并根据需要继续更新 ProgressBar。

这是一个缓慢的过程,有人告诉我,如果用户要加载应用程序并在初始化期间注意到延迟,这可能会也可能不会很理想。如前所述,列表数组为 58,当我的应用程序加载时,它需要先计算所有资产大小,然后再进行实际初始化。

我想让事情变得更快,只需设置所有变量而不使用 AsyncTask 和 ProgressBar,但我喜欢有一个介绍加载屏幕,就像 Google Play 中的所有其他应用一样。

因此,如何获取应用的总大小?

【问题讨论】:

  • 您是从手机本地加载文件/资产/内容,还是通过网络加载?此加载需要多长时间?
  • 我正在从手机本地加载我的资产/内容。加载时间总共大约需要 2 到 3 秒,包括初始化时间。在一些高端手机上大约需要 1 秒左右。
  • 我建议使用加载文件的数量来计算进度条的文件总数。使用文件大小是不必要的,如果可能的话,只会成为您计算的开销。
  • @RajeshJAdvani 感谢您提供有关使用加载文件数占文件总数的提示。它可能会起作用。如果无论如何都需要总文件大小,您知道获得它的方法吗?并请在下面发布您的答案。我不喜欢回答我自己的问题,因为你是回答者。你应该得到代表。

标签: java android size progress-bar assets


【解决方案1】:

不使用总文件大小,并根据加载的字节数计算进度(这可能会增加加载时间),只需计算加载的文件数,并根据文件总数计算进度。

【讨论】:

    猜你喜欢
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2020-08-21
    • 2017-06-03
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多