アプリケーションで使用する小さな 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;
}
これは非同期タスクで行われます。これの速度を改善する方法はありますか?ご意見ありがとうございます。