我有自定义ListAdapter
。我想为每个列表项下载图像。我尝试在AsyncTask
下载它们(我在其他讨论中找到了这个解决方案),但由于某种原因我总是收到通信"SkImageDecoder::Factory returned null"
public class MainMenuListAdapter extends BaseAdapter {
private List<Event> _events;
private Context _activityContext;
public MainMenuListAdapter(List<Event> events, Context context) {
this._events = events;
this._activityContext = context;
}
@Override
public int getCount() {
return this._events.size();
}
@Override
public Object getItem(int position) {
return this._events.get(position);
}
@Override
public long getItemId(int position) {
return this._events.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
LayoutInflater inflater = (LayoutInflater) this._activityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.mainmenu_list_item, parent,false);
}
Event e = (Event)getItem(position);
new DownloadImageTask((ImageView)convertView.findViewById(R.id.imageView1))
.execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");
TextView tvName = (TextView)convertView.findViewById(R.id.name);
tvName.setText(e.getName());
TextView tvDescription = (TextView)convertView.findViewById(R.id.description);
tvDescription.setText(e.getDescription());
TextView tvDate = (TextView)convertView.findViewById(R.id.date);
tvDate.setText(e.getDate());
TextView tvAddress = (TextView)convertView.findViewById(R.id.address);
tvName.setText(e.getAddress());
return convertView;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
String uri = Uri.parse(urldisplay)
.buildUpon()
.appendQueryParameter("key", "val")
.build().toString();
Bitmap mIcon11 = null;
try {
URL url = new URL(uri);
InputStream in = url.openStream();
mIcon11 = BitmapFactory.decodeStream(in);
return mIcon11;
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}