我正在尝试从多个URL获取位图。我正在使用以下代码。
Bitmap getBitmapFromUrl(String src){
try{
URL url = new URL(src);
Log.d(TAG, "bitmap url is : " + url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
Log.d(TAG, "getBitmapFromUrl1: ");
connection.setDoInput(true);
Log.d(TAG, "getBitmapFromUrl2: ");
connection.connect();
Log.d(TAG, "getBitmapFromUrl3: ");
InputStream inputStream = connection.getInputStream();
Log.d(TAG, "getBitmapFromUrl4: ");
return BitmapFactory.decodeStream(inputStream);
}catch (Exception e){
Log.e(TAG, "getBitmapFromUrlException " + e );
return null;
}
}
但是第一次调用connection.connect()
时执行时间太长。我在AsyncTask
类中使用此代码。