我需要從服務器獲取圖像並將其加載到圖像按鈕。當應用程序片段啓動時,動態加載的圖像超過50張。什麼是最好的方式來做到這一點。目前我使用下面的代碼,當我運行它的應用程序被卡住了,我也跟着Android Loading Image from URL (HTTP)教程,但它不顯示圖像時,有多個圖像。如何從圖像加載圖像到ImageButton
這是代碼我目前使用的時候,但申請凍結
private void setListParentItemInfo(View convertView,final IPTVChannel iptvChannel){
ImageButton logoBtn = ((ImageButton) convertView.findViewById(R.id.channelLogo));
String image_url="http://Channel.com.au/ChannelLogos/"+Channel.getLogoName();
logoBtn.setImageBitmap(downloadBitmap(image_url));
}
private Bitmap downloadBitmap(String url) {
// initilize the default HTTP client object
final DefaultHttpClient client = new DefaultHttpClient();
//forming a HttoGet request
final HttpGet getRequest = new HttpGet(url);
try {
HttpResponse response = client.execute(getRequest);
//check 200 OK for success
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode +
" while retrieving bitmap from " + url);
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
// getting contents from the stream
inputStream = entity.getContent();
// decoding stream data back into image Bitmap that android understands
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (Exception e) {
// You Could provide a more explicit error message for IOException
getRequest.abort();
Log.e("ImageDownloader", "Something went wrong while" +
" retrieving bitmap from " + url + e.toString());
}
return null;
}
我怎樣才能解決這個或其他任何理由這樣做呢?
我建議使用[畢加索庫(http://square.github.io/picasso/) –
本教程將幫助你實現畢加索圖書館[使用Android圖像庫畢加索](https://androidcodegeeks.com/working-with-android-image-library-picasso/) –