0

My code hits error, java.lang.OutOfMemory at

xxx.adapter.FeedListAdapter.getImageBitmap(FeedListAdapter.java) and xxx.adapter.FeedListAdapter.getView(FeedListAdapter.java) for the below lines in the code:

mholder.imgImageView.setImageBitmap(getImageBitMap(mfeeds.getImageByte()));
return (_imagebyte==null?null:BitmapFactory.decodeByteArray(_imagebyte, 0, _imagebyte.length));

Entire class with lines hit the error:

public class FeedsListAdapter extends BaseAdapter {
private Context mContext;
private List<SportFeed> mFeedsModels;

public FeedsListAdapter(Context context,List<SportFeed> list) {
    this.mContext = context;
    this.mFeedsModels = list;
}

private class ViewHolder{
    FrameLayout imgLayout;
    ImageView imgImageView;
    TextView txtTitle;
    TextView txtDesc; 
    TextView txtDateTime;
    LinearLayout ly_content;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder mholder = null;
    LayoutInflater minflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(convertView == null){ 
        convertView = minflater.inflate(R.layout.feeds_listing_row, null);
        mholder = new ViewHolder();
        mholder.ly_content = (LinearLayout)convertView.findViewById(R.id.flr_content_layout);
        mholder.imgLayout = (FrameLayout)convertView.findViewById(R.id.flr_img_layout);
        mholder.imgImageView = (ImageView)convertView.findViewById(R.id.flr_img);
        mholder.txtTitle = (TextView)convertView.findViewById(R.id.flr_textTitle);
        mholder.txtDesc = (TextView)convertView.findViewById(R.id.flr_desc);
        mholder.txtDateTime = (TextView)convertView.findViewById(R.id.flr_datetime);

    }else{
        mholder = (ViewHolder)convertView.getTag();
    }

    SportFeed mfeeds = (SportFeed)getItem(position);
    mholder.txtTitle.setText(mfeeds.getTitle());
    String shortDesc = mfeeds.getContentString();
    mholder.txtDesc.setText((shortDesc.length()>0?mfeeds.getContentString():""));
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)mholder.ly_content.getLayoutParams();
    if(mfeeds.getImageUrl().trim().equals("")){
        mholder.imgLayout.setVisibility(View.GONE);
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
    }else{
        mholder.imgLayout.setVisibility(View.VISIBLE);
---->   mholder.imgImageView.setImageBitmap(getImageBitMap(mfeeds.getImageByte())); //this line hits error
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
    }
    mholder.txtDateTime.setText(StringUtils.postDateStringConverter(mContext,mfeeds.getPostDate()));
    return convertView;
}

private Bitmap getImageBitMap(byte[] _imagebyte){

----> return (_imagebyte==null?null:BitmapFactory.decodeByteArray(_imagebyte, 0, _imagebyte.length)); //this line hits error
}

LogCat:

08-14 03:00:40.384: E/AndroidRuntime(1506): FATAL EXCEPTION: main
08-14 03:00:40.384: E/AndroidRuntime(1506): Process: org.diebao.rbbb, PID: 1506
08-14 03:00:40.384: E/AndroidRuntime(1506): java.lang.OutOfMemoryError
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:500)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:523)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at org.diebao.rbbb.adapter.FeedsListAdapter.getImageBitMap(FeedsListAdapter.java:84)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at org.diebao.rbbb.adapter.FeedsListAdapter.getView(FeedsListAdapter.java:74)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.widget.AbsListView.obtainView(AbsListView.java:2240)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.widget.ListView.onMeasure(ListView.java:1175)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.view.View.measure(View.java:16497)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)            
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.view.View.measure(View.java:16497)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.view.View.measure(View.java:16497)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.view.View.measure(View.java:16497)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.view.View.measure(View.java:16497)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.view.View.measure(View.java:16497)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.view.Choreographer.doFrame(Choreographer.java:544)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.os.Handler.handleCallback(Handler.java:733)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.os.Handler.dispatchMessage(Handler.java:95)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.os.Looper.loop(Looper.java:136)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at android.app.ActivityThread.main(ActivityThread.java:5017)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at java.lang.reflect.Method.invokeNative(Native Method)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at java.lang.reflect.Method.invoke(Method.java:515)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-14 03:00:40.384: E/AndroidRuntime(1506):     at dalvik.system.NativeStart.main(Native Method)
08-14 03:00:40.574: W/ActivityManager(383):   Force finishing activity org.diebao.rbbb/.activity.FeedListing
08-14 03:00:40.614: W/ActivityManager(383):   Force finishing activity org.diebao.rbbb/.activity.CategoryListing

Note: I have searched similar issues but i couldn`t find one really similar or very close. Herewith, the function implementation is not wrong because it works with many devices with no issue, it is just some certain Android devices hit the error. Maybe i need to change some part of coding to make it smooth and make the app reliable widely.

Thank you for help.

4
  • try large heap = true in manifest under application tag...may be useful not sure Commented Aug 14, 2014 at 8:26
  • the image you're loading might be too big, check this link out:developer.android.com/training/displaying-bitmaps/… Commented Aug 14, 2014 at 8:27
  • i have come across this type of situations by writing some lines for handling large bitmaps.....using Matrix class. Commented Aug 14, 2014 at 8:28
  • i tried android:largeHeap="true" in Manifest but it didnt work. Commented Aug 14, 2014 at 8:39

1 Answer 1

1

Bitmap handling is not trivial in Android. It's not possible to point on a line of code and tell you to just change it and will work. There are many aspects to be taken care of.

The Android docs are really good in giving a good overview what to take into consideration. A few points are:

The tip for using android:largeHeap is really discouraged as it is a hack and considered bad practice in nearly all but the most extreme cases. Usually you should code your app memory efficent, instead of just using more memory.

For an easy implementaiton for most of this issues I recommend looking into Picasso, which will take care of loading, downscaling and caching for you, with only a single line of code.

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

4 Comments

It is a clear explanation, thank you. But i am not excel enough to go through each one of your links and find most flexible way to implement in my code. I ll be glad if you can give me some specific tips based on my question above.
Just read the answer, go with picasso. But I also urge you to read and understand the topics in the android docs as it is vital to understand how things work in android. If you don't like using another lib, you have to implement a lru bitmap cache yourself (or take code snippets) and take care of loading the bitmaps (but then you have to read the docs)
the function implementation is not wrong because it works with many devices with no issue, it is just some certain Android devices hit the error. Maybe i need to change some part of coding to make it smooth and make the app reliable instead using another method or methods.
If your memory design fails on a single device there is a flaw in your code. The point is you have to take the different environnents into consideration (a 320x480 screen device has usally less RAM but also rarely needs full resolution images). Thats why I said its not trivial and cant be solves with a single line of code. I wouls start by implementing an lru cache and\or downscaling images to the needed resolution if they are not resources.

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.