Question

İ am using this custom adapter for showing youtube thumbnail.

public class MyMedyaAdapter extends SimpleAdapter {
    Context context;
    int layout;
    List<? extends Map<String, ?>> data;
    String[] from;
    int[] to;

    String resimURL = "http://img.youtube.com/vi/{0}/0.jpg";

    public MyMedyaAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
        this.context = context;
        this.layout = resource;
        this.data = data;
        this.from = from;
        this.to = to;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(layout, parent, false);

        TextView metin = (TextView) rowView.findViewById(R.id.row_medya_text);

        String baslik = data.get(position).get(from[0]) + "";
        baslik = baslik.replace(".pdf", "");

        metin.setText(baslik);

        if (from.length > 1) {
            final ImageView resim = (ImageView) rowView.findViewById(R.id.row_medya_image);

            final int p2 = position;

            String resimID = data.get(p2).get(from[1])+"";

            String rowImage = resimURL.replace("{0}", resimID);
            try {
                Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(rowImage).getContent());
                resim.setImageBitmap(bitmap);

            } catch (Exception e) {
                // TODO: handle exception
            }
        }

        return rowView;
    }
}

adding adapter to listview with this code;

public class VideoThered extends AsyncTask<Void, Void, MyMedyaAdapter> {

        @Override
        protected MyMedyaAdapter doInBackground(Void... params) {

            List<HashMap<String, String>> response = new ArrayList<HashMap<String, String>>();

            try {
                                //JSON request
                response = new JsonHelper().GetVideoEntitys();
            } catch (Exception e) {
                response = null;
            }

            if (response != null) {
                adapter = new MyMedyaAdapter(ctx, response, R.layout.row_medya, from, to);
            }
            else
            {
                adapter = null;
            }
            return adapter;
        }

        @Override
        protected void onPostExecute(MyMedyaAdapter result) {
            if (result != null) {
                                //StrictMode$AndroidBlockGuardPolicy.onNetwork() error with this line on honeycomb
                listVideolar.setAdapter(adapter);
            }
        }


    }

i am getting /StrictMode$AndroidBlockGuardPolicy.onNetwork() error.

How can i manage to show row images from web and not getting this error?

i am using listview main data from AsyncTask. in this data list, each item has a unique youtube video id. with this id i am getting video thumbnails from web. but on my custom adapter did not use AsyncTask. i know that's the problem. But how can i manage to my custom adaptor to use AsyncTask?

Was it helpful?

Solution

You are getting the error because you do network access in the UI thread. This is considered bad, because network access takes a long time and thus blocks the UI.

To solve this, you can write an AsyncTask that pulls the data from the net within its doInBackground method and then in onPostExecute fills the view with the bitmap/drawable. I think the Android documentation has an example for this scenario, but I can't find it right now.

You can also have a look at this source code snippet, DownloadUserImageTask from my Zwitscher app; the url is retrieved from the passed User object. The picture is then added to the view in

userPictureView.setImageBitmap(result);   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top