我正在尝试下载将在我的应用程序中使用的大量小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;
}
这是在异步任务中完成的。有什么办法可以提高速度吗?感谢您的任何意见!