0

我遇到问题了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;
            }


        }
4

1 回答 1

0

当应用程序尝试在 HoneyComb 或更高版本的主线程上执行网络操作时,会引发此异常......因此,为了在列表视图中执行图像的异步加载,您可以使用延迟加载概念....有很多图书馆……请找到一些……的链接

https://github.com/nostra13/Android-Universal-Image-Loader

ListView 中图像的延迟加载

希望能帮助到你

于 2013-02-16T08:23:56.297 回答