我需要从服务器获取图像并将其加载到图像按钮。当应用程序片段启动时,动态加载的图像超过50个。最好的方法是什么。目前我使用下面的代码,当我运行它时,应用程序卡住了,我也跟着Android Loading Image from URL (HTTP)教程,但是当有多个时,它没有显示图像图像。
这是我正在使用的代码,但应用程序正在冻结
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;
}
如何解决此问题或其他原因?
答案 0 :(得分:4)
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();
Picasso.with(context).load(image_url).placeholder(yourDrawableImage).into(logoBtn);
}
答案 1 :(得分:3)
使用picaso库,它易于使用且快速并节省现金以供进一步使用。
答案 2 :(得分:2)
是的,它正在冻结,因为它在与UI(主线程)相同的线程中运行。始终使用不同的线程(例如AsyncTask)从网络加载某些东西,较新的API级别根本不允许您这样做。 但在这种情况下,绝对应该使用Picasso库。
答案 3 :(得分:2)
如果您使用小图片,则可以使用如下的异步任务加载图像。如果您有大图片,则需要关注http://developer.android.com/intl/es/training/displaying-bitmaps/load-bitmap.html
class ImageLoader extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... params) {
return getBitmapFromURL(params[0]);
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
imageButton.setImageBitmap(result);
}
}
public Bitmap getBitmapFromURL(String imageURL) {
try {
URL url = new URL(imageURL);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
// Log exception
return null;
}
}
答案 4 :(得分:0)
答案 5 :(得分:0)
使用Facebook等壁画库。 http://frescolib.org/
使用此功能,您无需从外部下载图像。 只需设置通过uri的图像。它会自动下载并将其设置为视图。