Reputation: 5951
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.
Upvotes: 0
Views: 477
Reputation: 35234
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.
Upvotes: 1