Java 安卓:我如何获得应用程序';总大小是多少?

Java 安卓:我如何获得应用程序';总大小是多少?,java,android,size,progress-bar,assets,Java,Android,Size,Progress Bar,Assets,在安卓系统中,我看到很多应用程序都有加载屏幕。我知道ProgressBar实现了AsyncTask方法,但我从未见过有人知道他们是如何获得应用程序总大小的值的,或者如果他们正在加载资产,则知道总资产大小 通过动态获取应用程序的总大小,我将能够在发布时或发布后向应用程序添加其他内容。然后我就可以计算应用程序加载的总进度 到目前为止,我做了一个变通方法,如下所示: package nttu.edu.activities; import java.io.ByteArrayOutputStream;

在安卓系统中,我看到很多应用程序都有加载屏幕。我知道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();
    }
}
包nttu.edu.activities;
导入java.io.ByteArrayOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.util.LinkedList;
导入java.util.Queue;
进口nttu.edu.R;
导入nttu.edu.graphics.Art;
导入android.app.Activity;
导入android.content.Intent;
导入android.content.res.AssetManager;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.os.Handler;
导入android.widget.ProgressBar;
公共类NewLoadingActivity扩展活动{
公共酒吧;
私人资产管理人资产管理人;
公共经办人;
公共任务;
私有最终字符串[]列表={
//艺术精灵
“art/sprites.png”,…};
私有类ProgressTask扩展了AsyncTask{
公共int totalByteSize;
公共int currentByteSize;
公共队列位图队列;
公共队列字节队列;
公共事务处理任务(){
totalByteSize=0;
currentByteSize=0;
bitmapQueue=新的LinkedList();
byteQueue=新的LinkedList();
}
公共void onPostExecute(void参数){
Art.sprites=bitmapQueue.remove();
完成();
}
公共无效onPreExecute(){
试一试{
for(int i=0;i0){
byte[]bytes=byteQueue.remove();
位图位图=位图工厂.decodeByteArray(字节,0,字节.长度);
添加(位图);
currentByteSize+=字节数。长度;
这是一种进步;
}
}
捕捉(中断异常e){
e、 printStackTrace();
}
}
返回null;
}
私有字节[]readFromStream(字符串路径)引发IOException{
ByteArrayOutputStream输出=新建ByteArrayOutputStream();
字节[]缓冲区=新字节[1024];
整数长度=0;
InputStream输入=assetManager.open(路径);
while(input.available()>0&&(length=input.read(buffer))!=-1)
输出.写入(缓冲区,0,长度);
返回output.toByteArray();
}
}
创建时的公共void(Bundle b){
super.onCreate(b);
这个.setContentView(R.layout.progressbar);
assetManager=this.getAssets();
handler=新的handler();
task=新的ProgressTask();
bar=(ProgressBar)this.findviewbyd(R.id.loadingBar);
if(bar==null)抛出新的RuntimeException(“加载进度条失败”);
task.execute();
}
公共空间整理(){
意向意向=新意向(此,MenuActivity.class);
intent.putExtra(“成功标志”,Art.sprites!=null);
此.setResult(结果正常,意图);
super.finish();
}
}
这项活动唯一的主要问题是,我必须首先在顶部附近的列表中添加新资产。列表数组大小为58:

private final String[]list={“art/sprites.png”,…}

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

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

我希望通过设置所有变量而不使用AsyncTask和ProgressBar来加快速度,但我希望有一个简介加载屏幕,就像Google Play中的所有其他应用一样


因此,如何获得应用程序的总大小?

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

您是从手机本地加载文件/资产/内容,还是通过网络加载?加载需要多长时间?我正在从手机本地加载我的资产/内容。加载时间总计约为2到3秒,包括初始化时间。在一些高端手机上大约需要1秒左右的时间。我建议使用加载的文件数来计算进度条的总文件数。使用文件大小是不必要的,并且只会成为您计算的开销,如果