1

我制作了一个 android 应用程序,当网络不可用时,图像将保存到本地数据库。我将图像 uri 保存在数据库表中,并从图像 uri 中检索图像,并在使用自定义数组适配器时显示该imageView图像listView 。当我使用模拟器运行我的应用程序时,它运行良好。但是,在使用 android 手机时,它显示Forced Close 错误

错误日志如下:

java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:504)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:370)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:715)
at android.graphics.drawable.Drawable.createFromStream(Drawable.java:675)
at android.widget.ImageView.resolveUri(ImageView.java:525)
at android.widget.ImageView.setImageURI(ImageView.java:309)
at com.freedom.net.Syncho2$NoteListAdapter.getView(Syncho2.java:219)
at android.widget.AbsListView.obtainView(AbsListView.java:1519)
at android.widget.ListView.makeAndAddView(ListView.java:1749)
at android.widget.ListView.fillDown(ListView.java:674)
at android.widget.ListView.fillFromTop(ListView.java:731)
at android.widget.ListView.layoutChildren(ListView.java:1602)
at android.widget.AbsListView.onLayout(AbsListView.java:1349)
at android.view.View.layout(View.java:7320)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1263)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1137)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1051)
at android.view.View.layout(View.java:7320)
at android.widget.FrameLayout.onLayout(FrameLayout.java:342)
at android.view.View.layout(View.java:7320)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1263)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1137)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1051)
at android.view.View.layout(View.java:7320)
at android.widget.FrameLayout.onLayout(FrameLayout.java:342)
at android.view.View.layout(View.java:7320)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1162)

我的自定义适配器如下所示:

public class NoteListAdapter extends  ArrayAdapter<ImageFromDB> {
Context c;

ArrayList<ImageFromDB> image = null;
ArrayList<String> uriImage = null;
ArrayList<Integer> ID = null;

private AllId allid = null;
DBAdapter db = null;

WatchListAllEntity watchListAllEntity=null;
int flagVariable=1;
private ArrayList<ImageFromDB> items;

public NoteListAdapter(Context context, int textViewResourceId,
        ArrayList<ImageFromDB> items) {

    super(context, textViewResourceId, items);
    Log.e("sf","123");
    this.items = items;
    c=context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    final int myPosition = position;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.listitempict2, null);
    }
    ImageFromDB re = items.get(position);
    Log.e("re", re+"");
    if (re != null) {
        ImageView tt = (ImageView) v.findViewById(R.id.imageviewproduct);
        String imageUri = re.getImageuri();
        Log.e("imageUri", imageUri+"");
        tt.setImageURI(Uri.parse(imageUri));

        final CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkboxproduct);
        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {

                if (isChecked) {
                    if(flagVariable==1)
                    {
                        String ImUri = items.get(myPosition).getImageuri();
                        allid.setImageUri(ImUri);
                        allid.setId(items.get(myPosition).getId());
                        flagVariable++;
                        Log.e("flagVariable  : " , flagVariable+"");
                    }
                    else
                    {
                        checkBox.setChecked(false);
                        //Toast.makeText(Syncho2.this,"正しく選んでください。",Toast.LENGTH_SHORT).show();
                    }
                } else {
                    flagVariable=1;
                    Log.e("flagVariable  : " , flagVariable+"");
                }
            }
        });
    }
    return v;
}
}

我从这样的活动中调用此适配器

noteListAdapter = new NoteListAdapter(this, R.layout.listitempict, image);

我从调试中注意到,当图像只有两个时,适配器被调用了六次它五次返回视图,但第六次显示该错误。

4

3 回答 3

2

阅读此内容:http: //developer.android.com/training/displaying-bitmaps/load-bitmap.html ,您将能够毫无错误地加载您的图像!

于 2012-08-02T10:40:28.720 回答
0
 public View getView(int position, View convertView, ViewGroup parent) {        

        View row = convertView;
     // if it's not recycled, initialize some attributes
        if (row == null) {  
            LayoutInflater li = (LayoutInflater)   mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
            row = li.inflate(R.layout.menucategory, null);          
            //imageView.setLayoutParams(new GridView.LayoutParams(85, 85));         
        }
            // getCategoryItem          
            Category  category = (Category) getItem(position);
            // Get reference to ImageView
            categoryIcon = (ImageView) row.findViewById(R.id.category_image);
            //categoryIcon.setLayoutParams(new GridView.LayoutParams(85, 85));
            categoryIcon.setScaleType(ImageView.ScaleType.CENTER_CROP);
            categoryIcon.setPadding(5, 5, 5, 5);

            categoryName = (TextView) row.findViewById(R.id.category_name);
            //Set category name
            categoryName.setText(category.getCategoryName());           


            if(category.getCategoryPath() != null){             
                    Bitmap bitmap = BitmapFactory.decodeFile(category.getCategoryPath());
                    System.out.println("bitmap2::  "+ bitmap);
                    if(bitmap != null){
                     categoryIcon.setImageBitmap(bitmap);
                    }               
            }                              

        return row;
    }
于 2012-08-02T11:02:49.497 回答
0

我找到了解决方案。我觉得每个人都应该知道关于 android 应用程序的内存管理。http://www.youtube.com/watch?v=_CruQY55HOk。当我试图直接在 ImageView 上显示图像 Uri 时,问题就出现了。我觉得每个人都应该避免像这样的代码 ImageView.setImageURI(Uri.parse(imageUri)) 。取而代之的是,尝试在 imageview 上加载位图或可绘制。

于 2012-09-10T10:25:13.330 回答