我遇到问题了connection.connect();
android.os.NetworkOnMainThreadException in android
我收到此错误如何将 asychtask 设置为此图像适配器我无法在 imageview 中显示该 url 图像
public class ImageAdapter extends BaseAdapter {
private Context context;
public static final String[] videos={
"http://img.youtube.com/vi/QshNtQsiL7Y/1.jpg","http://img.youtube.com/vi/5_-tL63516w/1.jpg",
"http://img.youtube.com/vi/CDEg9npvBa0/1.jpg"};
private ImageView imageview;
public ImageAdapter(Context c) {
// TODO Auto-generated constructor stub
context=c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return videos.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return videos[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageview = new ImageView(context);
imageview.setImageBitmap(getBitmapFromURL(videos[position]));
return imageview;
}
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap mybitmap = BitmapFactory.decodeStream(input);
return mybitmap;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}