且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

网格视图crashe上滚动太多的图像应用程序

更新时间:2023-10-19 16:33:10

getview方法使用该code到,同时设置位图的ImageView

 私人位图德codeURI(URI selectedImage)抛出FileNotFoundException异常
 {
            BitmapFactory.Options O =新BitmapFactory.Options();
            o.inJustDe codeBounds = TRUE;
            BitmapFactory.de codeStream(
                    。getContentResolver()openInputStream(selectedImage),空,O);            最终诠释REQUIRED_SIZE = 800;            INT width_tmp = o.outWidth,height_tmp = o.outHeight;
            int标= 1;
            而(真){
                如果(width_tmp / 2'; REQUIRED_SIZE || height_tmp / 2版; REQUIRED_SIZE)
                {
                    打破;
                }
                width_tmp / = 2;
                height_tmp / = 2;
                规模* = 2;
            }            BitmapFactory.Options O2 =新BitmapFactory.Options();
            o2.inSampleSize =规模;`输入code here`
            返回BitmapFactory.de codeStream(
                    getContentResolver()openInputStream(selectedImage),空,O2)。
        }

in my project that uses custom adapter with grid view , when my grid view load many Items that contain images on scrolling my activity crashes and reloads again. it had some resource problem on loading image that i solved with help of @Raghunandan my error is out of memmory. cause i think loadfull size images.

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;
    }

Use this code to in your getview method while setting bitmap in 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);
        }