I am loading images into a grid view from a server. For this, I'm using Universal Image loader. I am using the image loader inside the getview() method of the adapter. It takes the URL of the image from arraylist & loads the image from server. Images can be in 100's. I am displaying the grid view in a fragment so for each fragment there is a separate grid view. When I switch between multiple fragments for a long time I no longer get the out of memory error.
Suggested solution:
- Release the unused bitmap.
So I am loading the images using third party library. I am just passing the image view in the calling function so how can I release the bitmap?
- Make heap large true in manifest
I guess heap memory is dynamic & whenever there is an increase in memory garbage collector will free the memory.
3.Use UIL/Picasso/Glide to avoid memory leaks
4.Code to release the memory of fragment onDestroy() method ()
private void unbindDrawables(View view)
{
if (view.getBackground() != null)
{
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup && !(view instanceof AdapterView))
{
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++)
{
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
}
I am already using a third party library.
Here is the code for my gridViewAdapter class.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View view = convertView;
if (view == null)
{
view = inflater.inflate(R.layout.grid_item_photo, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView) view.findViewById(R.id.imgLoader);
holder.play = (ImageView) view.findViewById(R.id.play);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
}
try{
if(IMAGES_LIST.get(position).getType().equals("video"))
{
holder.play.setVisibility(View.VISIBLE);
}
else
{
holder.play.setVisibility(View.GONE);
}
ImageLoader.getInstance().displayImage(AppConst.BASE_IMAGE_URL+IMAGES_LIST.get(position).getThumbnail(), holder.imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
view.setClickable(true);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
view.setClickable(true);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
view.setClickable(false);
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
}
});
}catch(Exception e)
{
}
catch(OutOfMemoryError e)
{
}
return view;
}
static class ViewHolder {
ImageView imageView,play;
}
}