0

i am getting problem at connection.connect();

android.os.NetworkOnMainThreadException in android

i am getting this error how to set asychtask to this imageadapter i unable to display that url image in imageview

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;
            }


        }
Deepzz
  • 4,573
  • 1
  • 28
  • 52
priya madhuri
  • 41
  • 1
  • 4
  • 1
    see http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception post for NetworkOnMainThreadException – ρяσѕρєя K Feb 16 '13 at 07:57
  • which version of Android u are using for current project? – ρяσѕρєя K Feb 16 '13 at 08:17
  • 4.2 version i am using – priya madhuri Feb 16 '13 at 08:33
  • then add `StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);` in onCreate of Activity just after setContentView or before add adapter to LisView and see [this](http://android-developers.blogspot.in/2010/12/new-gingerbread-api-strictmode.html) tutorial for more help – ρяσѕρєя K Feb 16 '13 at 08:53

1 Answers1

0

this exception is thrown when an application attempts to perform a networking operation on its main thread in HoneyComb or later...So in order to perform the async Loading of images in the list view you can use Lazy loading concept.... there is alot of libraries for it...Please find the link of some....

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

Lazy load of images in ListView

Hope it helps

Community
  • 1
  • 1
Karan_Rana
  • 2,813
  • 2
  • 26
  • 35
  • how to add async task to this imageadapter – priya madhuri Feb 16 '13 at 08:37
  • See the concept of the async task is same as creating a background thread which the above mentioned api's are using...I can give you an idea how you can use a async task to load images....The constructor of async task would have imageview object and url of image and store that in Hash map and in doInBackgournd() you can convert the url to bitmap and in the postexecute method set the bitmap in the imageview by fetching the relatede image view from hasmap...... – Karan_Rana Feb 16 '13 at 08:42