0

i am developing an application that uses staggered gridview i am having serious performance issues as after loading just 6,7 items the scrolling is very laggy and half the time i am getting: OutOfMemoryError at "android.graphics.BitmapFactory.nativeDecodeStream(Native Method)"

this is my adapter (i am using the holder pattern):

package ngapps.socialbackground;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import ngapps.socialbackground.Util.ImageDownloaderTask;

/**
 * Created by Naor on 02/09/2014.
 */
public class ItemAdapter extends BaseAdapter {

    ArrayList<ImageItem> imageItemArrayList = new ArrayList<ImageItem>();
    Context context;

    static class ViewHolder {
        public TextView textView;
        public ImageView imageView;

        ViewHolder(View base) {
            textView = (TextView) base.findViewById(R.id.textView);
            imageView = (ImageView) base.findViewById(R.id.imageView);
        }
    }

    public ItemAdapter(Context context){
        this.context = context;
    }

    @Override
    public int getCount() {
        return imageItemArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return imageItemArrayList.get(position);


    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void addItem(ImageItem imageItem){
        imageItemArrayList.add(imageItem);
        notifyDataSetChanged();
    }


    public void addItems(List<ImageItem> items) {
        imageItemArrayList.addAll(items);
        notifyDataSetChanged();
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ViewHolder holder;
        if (convertView == null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.item_layout,null);
            holder = new ViewHolder(v);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }
        ImageItem curImageItem = imageItemArrayList.get(position);
        holder.textView.setText(curImageItem.getText());
        if (curImageItem.getPreviewBitmap() != null)
            holder.imageView.setImageBitmap(curImageItem.getPreviewBitmap());
        else
            new ImageDownloaderTask(holder.imageView, curImageItem).execute(curImageItem.getImageURL());

        return  v;

    }
}

and this is ImageDownloaderTask:

package ngapps.socialbackground.Util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

import java.io.InputStream;

import ngapps.socialbackground.ImageItem;

/**
 * Created by Naor on 04/09/2014.
 */
public class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;
    ImageItem imageItem;
    public ImageDownloaderTask(ImageView bmImage, ImageItem imageItem) {
        this.bmImage = bmImage;
        this.imageItem = imageItem;
    }

    protected Bitmap doInBackground(String... urls) {
        String url = urls[0];
        Bitmap mIcon = null;
        try {
            InputStream in = new java.net.URL(url).openStream();
            mIcon = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
        }
        return mIcon;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
        imageItem.setPreviewBitmap(result);
    }
}

the images are around 160 pixels wide, i have verified that the holder pattern works and i am only creating a finite amount of Views and no new ImageDownloaderTask are created after the images finished downloading

and still it lags after everything has finished loading when scrolling

what is hurting my performance?

Community
  • 1
  • 1
user1333057
  • 607
  • 1
  • 8
  • 19

1 Answers1

0

You have a memory leak on your code, I post a solution and hints to deal with this problem here. Probably the OutOfMemoryException happend in this lines of your code:

InputStream in = new java.net.URL(url).openStream();
mIcon = BitmapFactory.decodeStream(in);
Community
  • 1
  • 1
Jose Rodriguez
  • 9,753
  • 13
  • 36
  • 52