0

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;
    }
}
Xhulio
  • 581
  • 7
  • 26
  • 1
    Possible duplicate of [Strange out of memory issue while loading an image to a Bitmap object](http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object) – Dmytro Rostopira Jan 10 '17 at 08:56
  • 1
    you can try out picasso with its fit() function your oom error won't appear here is reference link https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwiZ9LXqmrfRAhVEuY8KHfKIDqsQFggZMAA&url=http%3A%2F%2Fsquare.github.io%2Fpicasso%2F&usg=AFQjCNHtRDFJvt8HPDMbENo3RvpkdPZYsA&sig2=GxuKK26D5b7zeIP3m0ewbA&bvm=bv.143423383,d.c2I – pkgrover Jan 10 '17 at 08:57
  • 1
    try libraries Glide or Picasso , as they create individual threads to render your server images to imageview . – Rv Lalwani Jan 10 '17 at 09:07
  • post your code here , maybe we can help u – Quick learner Jan 10 '17 at 09:17

1 Answers1

1

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

Mayur Raval
  • 3,250
  • 6
  • 34
  • 57
  • I have tried with Picasso, but still it gives me OutOfMemory Issues. – Xhulio Jan 10 '17 at 14:07
  • 1
    @Xhulio, You can also try rest of two. If still problem persist there, then you should reduce size of images from the server. i think you should reduce there scale to down to overcome with this issue. – Mayur Raval Jan 11 '17 at 05:15