我需要从服务器获取图像并将其加载到图像按钮。当应用程序片段启动时,动态加载的图像超过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/) –