【问题标题】:Grid view crashe the app on scrolling on too many images网格视图在滚动太多图像时使应用程序崩溃
【发布时间】:2014-03-10 11:29:09
【问题描述】:

在我使用带有网格视图的自定义适配器的项目中,当我的网格视图加载许多包含滚动的图像的项目时,我的活动崩溃并再次重新加载。它在加载图像时遇到了一些资源问题,我在@Raghunandan 的帮助下解决了这些问题 我的错误是内存不足。因为我认为加载全尺寸图像。

public class MyAdapter extends ArrayAdapter<StructureCase> {

    private LayoutInflater mInflater           = null;
    public Context         context;
    public Class           distinationActivity = null;

    public MyAdapter(Context context, int textViewResourceId, List<StructureCase> objects) {
        super(context, textViewResourceId, objects);
        mInflater = LayoutInflater.from(context);
       // mInflater = (LayoutInflater)G.currentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public static class ViewHolder {
       public ImageView gem_img   = null;
       public TextView  gem_name   = null;
    }

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

        final StructureCase item = getItem(position);

        if (convertView == null) {

            convertView = this.mInflater.inflate(R.layout.my_grid_list,  null);
            //mInflater = (LayoutInflater)G.currentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.my_grid_list, parent, false);
            viewHolder = new ViewHolder();
            convertView.setTag(viewHolder);

            viewHolder.gem_img = (ImageView) convertView.findViewById(R.id.imageView_mygrid_list);
            viewHolder.gem_name = (TextView) convertView.findViewById(R.id.textView_mygrid_list);

            viewHolder.gem_name.setTypeface(G.typeFacePrs);

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

        int temp = 0;

        viewHolder.gem_name.setText(item.g_name);




int id = G.currentActivity.getResources().getIdentifier(item.g_image, "drawable", G.currentActivity.getPackageName());
        Drawable drawable = G.currentActivity.getResources().getDrawable(id);
        if(drawable != null){
        viewHolder.gem_img.setImageDrawable(drawable);
        }else{
            viewHolder.gem_img.setImageResource(R.drawable.almas);

        }



        //viewHolder.newsThumb.setImageResource(temp);

        viewHolder.gem_img.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        return convertView;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

【问题讨论】:

  • 我猜这是你有问题的地方Drawable drawable = G.currentActivity.getResources().getDrawable(id);
  • 在运行活动之前,我设置了 G.currentActivity ,因此我通过调用 G.currentActivity.this 在所有应用程序中设置了上下文。那我怎么解决呢?
  • 问题是没有找到可绘制资源。你也不需要这个convertView = this.mInflater.inflate(R.layout.my_grid_list, null);
  • 我更新了适配器并添加了“if”来检查drawable是否为空填充另一个图像。但还是有问题。
  • 什么是 MyAdapter 第 87 行?

标签: android image gridview out-of-memory adapter


【解决方案1】:

在您的getview method 中使用此代码,同时在ImageView 中设置位图。

private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException
 {
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(
                    getContentResolver().openInputStream(selectedImage), null, o);

            final int REQUIRED_SIZE = 800;

            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) 
                {
                    break;
                }
                width_tmp /= 2;
                height_tmp /= 2;
                scale *= 2;
            }

            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;`enter code here`
            return BitmapFactory.decodeStream(
                    getContentResolver().openInputStream(selectedImage), null, o2);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多