I need to fetch images from url's and Load them into gridview. I am getting network on Main thread exception in the getView method of my ImageAdapter class which extends Base Adapter. How can i solve this using a Separate thread or an Async Task.
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;
}
}