My code:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = new ImageView(mContext);
Drawable photo = new BitmapDrawable(loadBitmap(URL));
iv.setImageDrawable(photo);
iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
return iv;
}
private Bitmap loadBitmap(String url) {
try {
Bitmap bm = BitmapFactory.decodeStream((InputStream)this.fetch(url));
return bm;
}
catch(Exception e) {
e.printStackTrace();
return null;
}
}
public Object fetch(String address) {
try {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
catch(Exception e) {
e.printStackTrace();
}
return this;
}
I try to view about 100 photos with URL in gallery. But when view about 10 photos, the out of memory error was occur. What should I do to avoid the error?
I try to modify getView() as below.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = new ImageView(mContext);
InputStream is = (InputStream)this.fetch(URL);
Drawable photo = Drawable.createFromStream(is, "src");
iv.setImageDrawable(photo);
iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
return iv;
}
The memory trouble is still exist.
The error message is java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at line Drawable photo = Drawable.createFromStream(is, "src");
.