私のアプリでは、URL から取得した画像をダウンロードしています。ここでの問題は、時間がかかりすぎることです。画像は圧縮されており、サイズはわずか 9 km です。私は 50 個をダウンロードしているので、500kByte の画像がアプリによってダウンロードされているとしましょう。これはかなり速いはずですよね?そうではありません。ロードされるまで 20 秒間待機することがあります。何がそんなに時間がかかるのですか?画像をダウンロードする簡単な方法があります。ここにあります
int downloadingImages = getDownloadImageCount();
System.out.println(downloadingImages);
if(downloadingImages == 0){
for(int c = downloadingImages; c < 50; c++){
try{
bitmapArr[c] = getBitmapFromURL(imageURLArr[c]);
setDownloadImageCount(50);
publishProgress(c);
} catch (FileNotFoundException f1){
Log.v("FileNotFoundException", "Bitmap konnte nicht geladen werden");
} catch(NullPointerException e){
} catch (IllegalStateException ie){
}
setDownloadImageCount(50);
}
}
これは getBitmapFromUrl 関数です
public static Bitmap getBitmapFromURL(String src) throws FileNotFoundException {
try {
//Downloading the image
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
try{
//Saving the image to a bitmap and return it
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
//Catch Exception if file not found
} catch(FileNotFoundException ffe){
Log.v("FileNotFoundException", "getBitmapFromURLMethod");
return null;
}
//Catch Exception if error at input and output
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
つまり、何がそんなに時間がかかっているのですか?どうすれば速度を上げることができますか。後で画像のキャッシングを使用しますが、それでも...つまり、500kbsであり、非常に時間がかかります。何故ですか?