Xhulio
Xhulio

Reputation: 581

java.lang.OutOfMemoryError during scrolling of ListView android

I updated the application one day ago, and for the first time, I have received some crashes posted by users, like the following one:

java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:299)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.OutOfMemoryError
    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)
    at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper.loadBitmapFromStream(UrlImageViewHelper.java:109)
    at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper.access$100(UrlImageViewHelper.java:27)
    at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper$1.onDownloadComplete(UrlImageViewHelper.java:582)
    at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper$3.doInBackground(UrlImageViewHelper.java:648)
    at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper$3.doInBackground(UrlImageViewHelper.java:645)
    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    ... 3 more

I understand that this has to do with the processing of the images. (the app retrieves a lot of articles from the server which are displayed as an image and title. )

One the quick solution that I did is to reduce the size of images at maximum 600 px width (the images variable in size).

For this I have used com.koushikdutta.urlimageviewhelper library, version 1.0.4. I don't know, is there any other library that is able to process large amount of images, and how to use it.

One way how I use in ListView is like below:

public class NewsAdapter extends BaseAdapter {

    private ArrayList<NewsItem> data;

    private Context context;
    private LayoutInflater layoutInflater;

    public NewsAdapter(Context context, ArrayList<NewsItem> data) {
        this.data = data;
        layoutInflater = LayoutInflater.from(context);
        this.context = context;
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return data.get(position);
    }

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

    @SuppressLint({ "DefaultLocale", "InflateParams" })
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.news_row, null);
            holder = new ViewHolder();

            holder.image = (ImageView) convertView
                    .findViewById(R.id.image);

            holder.name = (TextView) convertView
                    .findViewById(R.id.time);

            holder.job = (TextView) convertView
                    .findViewById(R.id.title);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        //change the font
        Typeface typeFace=Typeface.createFromAsset(context.getAssets(),"fonts/Lato-Regular.ttf");

        holder.name.setTypeface(typeFace);
        holder.job.setTypeface(typeFace);

        UrlImageViewHelper.setUrlDrawable(holder.image,
                data.get(position).getImage_url());

        holder.name.setText( data.get(position).getDate());
        holder.job.setText( data.get(position).getTitle());

        return convertView;
    }

    static class ViewHolder {
        ImageView image;
        TextView name, job;
    }
}

Upvotes: 0

Views: 251

Answers (1)

Mayur Raval
Mayur Raval

Reputation: 3275

I think you have problem to decode stream into Bitmap, if there are lots streams to convert into bitmaps then there is high probability to throw out of memory.

If you have use library. try more

Nostra

Glide

Picasso

Upvotes: 1

Related Questions