Android 改进批量文件下载的方法?

Android 改进批量文件下载的方法?,android,performance,Android,Performance,我正在尝试下载大量的小png图像,这些图像将在我的应用程序中使用。它现在可以工作,但需要很长时间。我想知道是否有任何关于如何加快这个过程的建议,或者我的代码中是否存在明显的低效。现在的过程是下载一个JSON文件。这个JSON文件中有到包含图像的zip文件的链接。然后我解压文件并将图像存储在手机的内部存储器中。以下是到目前为止我得到的信息: @Override protected Object doInBackground(Object... params) {

我正在尝试下载大量的小png图像,这些图像将在我的应用程序中使用。它现在可以工作,但需要很长时间。我想知道是否有任何关于如何加快这个过程的建议,或者我的代码中是否存在明显的低效。现在的过程是下载一个JSON文件。这个JSON文件中有到包含图像的zip文件的链接。然后我解压文件并将图像存储在手机的内部存储器中。以下是到目前为止我得到的信息:

    @Override
    protected Object doInBackground(Object... params) {

        IS_UPDATING = true;
        final String input = Utils
                .downloadString("http://assets.json");

        if (input == null) {
            return null;
        }

        try {
            JSONObject data = new JSONObject(input);
            Iterator<String> topIterator = data.keys();
            while (topIterator.hasNext())
            {
                String topName = topIterator.next();
                String prefix = "png_";
                String preferences = null;
                JSONObject part_data = data.getJSONObject(topName);
                int map_version = part_data.getInt("VersionNumber");
                JSONObject url_json = new JSONObject(input).getJSONObject("Tiles").getJSONObject("TileURLs");
                Iterator<String> iterator = url_json.keys();
                while (iterator.hasNext())
                {
                    String temp = iterator.next();
                    //Downloads the zip file with map tiles
                    URL url = new URL(url_json.getString(temp));
                    InputStream is = url.openStream();
                    DataInputStream dis = new DataInputStream(is);
                    byte[] buffer = new byte[1024];
                    int length;

                    //Stores the zip file in app output
                    FileOutputStream fos = mContext
                            .openFileOutput("tiles_" + temp + ".zip",
                                    Context.MODE_PRIVATE);
                    while ((length = dis.read(buffer)) > 0)
                        fos.write(buffer, 0, length);
                    fos.close();
                    dis.close();
                    is.close();

                    //Extracts the images from the zip file
                    FileInputStream fis = mContext
                            .openFileInput("tiles_" + temp + ".zip");
                    ZipInputStream zis = new ZipInputStream(fis);
                    BufferedInputStream in = new BufferedInputStream(zis);
                    ZipEntry ze = null;

                    while ((ze = zis.getNextEntry()) != null)
                    {
                        String name = ze.getName();
                        if (!ze.isDirectory() && !name.contains("MACOSX"))
                        {
                            name = prefix + ze.getName();
                            name = name.replace('/', '_');
                            fos = mContext.openFileOutput(name, Context.MODE_PRIVATE);

                            for (int y = in.read(); y != -1; y = in.read()) {
                                fos.write(y);
                            }

                            zis.closeEntry();
                            fos.close();
                        }
                    }
                    in.close();
                    zis.close();
                    fis.close();
                    mContext.deleteFile("tiles_" + temp + ".zip");
                }

                sEditor.putInt(preferences, map_version);
                sEditor.commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finished = true;
        return null;
    }
@覆盖
受保护对象doInBackground(对象…参数){
是否更新=真;
最终字符串输入=Utils
.downloadString(“http://assets.json");
如果(输入==null){
返回null;
}
试一试{
JSONObject数据=新的JSONObject(输入);
迭代器topIterator=data.keys();
while(topIterator.hasNext())
{
字符串topName=topIterator.next();
String prefix=“png\”;
字符串首选项=null;
JSONObject part_data=data.getJSONObject(topName);
int map_version=part_data.getInt(“VersionNumber”);
JSONObject url_json=新的JSONObject(输入).getJSONObject(“Tiles”).getJSONObject(“tileURL”);
迭代器迭代器=url_json.keys();
while(iterator.hasNext())
{
String temp=iterator.next();
//下载带有贴图分幅的zip文件
URL=新URL(URL_json.getString(temp));
InputStream=url.openStream();
DataInputStream dis=新的DataInputStream(is);
字节[]缓冲区=新字节[1024];
整数长度;
//将zip文件存储在应用程序输出中
FileOutputStream fos=mContext
.openFileOutput(“tiles_u“+temp+”.zip“,
上下文。模式(私人);
而((长度=dis.read(缓冲区))>0)
fos.写入(缓冲区,0,长度);
fos.close();
dis.close();
is.close();
//从zip文件中提取图像
FileInputStream fis=mContext
.openFileInput(“tiles_u“+temp+”.zip”);
ZipInputStream zis=新的ZipInputStream(fis);
BufferedInputStream in=新的BufferedInputStream(zis);
ZipEntry ze=null;
而((ze=zis.getNextEntry())!=null)
{
String name=ze.getName();
如果(!ze.isDirectory()&&!name.contains(“MACOSX”))
{
name=前缀+ze.getName();
name=name.replace(“/”,“"”);
fos=mContext.openFileOutput(名称、上下文、模式_PRIVATE);
for(int y=in.read();y!=-1;y=in.read()){
fos.write(y);
}
zis.closeEntry();
fos.close();
}
}
in.close();
zis.close();
fis.close();
mContext.deleteFile(“tiles”+temp+.zip”);
}
sEditor.putInt(首选项,地图版本);
提交();
}
}捕获(例外e){
e、 printStackTrace();
}
完成=正确;
返回null;
}

这是在异步任务中完成的。我有什么办法可以提高这个速度吗?谢谢你的意见

是的,首先下载所有文件,然后解析它们


你可以做的另一件事是不要使用包装中的东西。是一个很好的选择。

我下载的zip文件大于1MB。如果临时目录上有警告,我应该担心这个问题吗?而且,我的大部分时间似乎都没有花在解析JSON上。我在应用程序的其他地方使用Gson,但在这里也将研究如何实现它。