在我的应用程序中,我正在下载从 URL 获取的图像。这里的问题是它花费的时间太长了。图像经过压缩,只有 9 公里大。我正在下载其中的 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;
}
}
我的意思是,什么需要这么长时间?我怎样才能提高速度。稍后我将使用cahin 来处理图像,但仍然......我的意思是它是500kbs 并且需要很长时间。这是为什么?