問題が発生していますconnection.connect();
android.os.NetworkOnMainThreadException in android
このエラーが発生しています このイメージアダプターに asychtask を設定する方法 私はイメージビューでその 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;
}
}