Bitmap工厂中的OutOfMemory错误和ListAdapter中的View

时间:2014-08-14 08:15:39

标签: android out-of-memory listadapter bitmapfactory

我的代码遇到错误,java /lang.OutOfMemory at

xxx.adapter.FeedListAdapter.getImageBitmap(FeedListAdapter.java)xxx.adapter.FeedListAdapter.getView(FeedListAdapter.java)代码中的以下行:

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

有线条的整个课程都会出错:

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

注意:我搜索过类似的问题,但我找不到一个非常相似或非常接近的问题。因此,功能实现没有错,因为它适用于许多没有问题的设备,只是某些Android设备遇到了错误。也许我需要更改编码的某些部分以使其流畅并使应用程序可靠地广泛应用。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

Android中的位图处理并不简单。它不可能指向一行代码并告诉您只需更改它并且可以正常工作。有许多方面需要注意。

Android文档非常适合概述要考虑的内容。有几点是:

使用android:largeHeap is really discouraged的提示因为它是一种黑客攻击,并且在几乎所有情况下都被认为是不好的做法。通常你应该编写应用程序内存效率,而不是仅使用更多内存。

对于大多数问题的简单实现,我建议查看Picasso,它将为您完成加载,缩减和缓存,只需一行代码。