我需要从 url 中获取图像并将它们加载到 gridview 中。我在扩展基本适配器的 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;
}
}