0

URLから画像を取得し、それらをグリッドビューにロードする必要があります。Base Adapter を拡張する ImageAdapter クラスの getView メソッドで、ネットワーク オン メイン スレッドの例外を取得しています。別のスレッドまたは非同期タスクを使用してこれを解決するにはどうすればよいですか。

public View getView(int position, View convertView, ViewGroup parent)   {           

    ImageView imageView;

    try {

            URL url = new URL(Product_ItemsURLs[position]);

            InputStream content = (InputStream)url.getContent();

            Drawable drawable = Drawable.createFromStream(content , "src"); 

            if (convertView == null)
            {
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(380,380));
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                imageView.setPadding(10, 10, 10, 10);
            }
            else
            {
                imageView = (ImageView) convertView;
            }

            imageView.setImageDrawable(drawable);

            return imageView;

        } catch (MalformedURLException e) {

            e.printStackTrace();
            return null;

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

}
4

1 に答える 1

0

以下の行が問題の原因です。ネットワーク操作を実行しています。ハニカム以降、Android は UI スレッドでのネットワーク操作を許可しません。

Drawable drawable = Drawable.createFromStream(content , "src");

を使用してドローアブルをダウンロードし、 (UI スレッドで実行される)Asynctaskビューにドローアブルを設定する必要があります。onPostExecute

于 2012-10-17T10:07:33.927 に答える