【问题标题】:Custom ListView adapter using URL image getting error 'StrictMode'使用 URL 图像的自定义 ListView 适配器出现错误“StrictMode”
【发布时间】:2012-09-27 07:24:36
【问题描述】:

我正在使用此自定义适配器来显示 youtube 缩略图。

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

使用此代码将适配器添加到列表视图;

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


    }

我收到 /StrictMode$AndroidBlockGuardPolicy.onNetwork() 错误。

我怎样才能设法显示来自网络的行图像而不出现此错误?

我正在使用来自 AsyncTask 的列表视图主要数据。 在这个数据列表中,每个项目都有一个唯一的 youtube 视频 ID。使用此 ID,我正在从网络上获取视频缩略图。但是在我的自定义适配器上没有使用 AsyncTask。我知道这就是问题所在。 但是我怎样才能让我的自定义适配器使用 AsyncTask?

【问题讨论】:

标签: android listview android-3.0-honeycomb android-strictmode


【解决方案1】:

您收到错误是因为您在 UI 线程中进行网络访问。这被认为是不好的,因为网络访问需要很长时间,因此会阻塞 UI。

要解决这个问题,您可以编写一个AsyncTask,在其doInBackground 方法中从网络中提取数据,然后在onPostExecute 中使用位图/可绘制对象填充视图。 我认为 Android 文档中有此场景的示例,但我现在找不到。

您也可以从我的 Zwitscher 应用程序中查看source code snippetDownloadUserImageTask; url 是从传递的 User 对象中检索的。然后将图片添加到视图中

userPictureView.setImageBitmap(result);   

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-06
    • 2013-07-30
    • 2012-09-22
    • 2015-07-09
    • 2020-07-22
    • 2016-01-01
    • 1970-01-01
    相关资源
    最近更新 更多