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;
    }
}
4
  • 1
    Possible duplicate of Strange out of memory issue while loading an image to a Bitmap object Commented Jan 10, 2017 at 8:56
  • 1
    you can try out picasso with its fit() function your oom error won't appear here is reference link google.co.in/… Commented Jan 10, 2017 at 8:57
  • 1
    try libraries Glide or Picasso , as they create individual threads to render your server images to imageview . Commented Jan 10, 2017 at 9:07
  • post your code here , maybe we can help u Commented Jan 10, 2017 at 9:17

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I have tried with Picasso, but still it gives me OutOfMemory Issues.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.