0

I have a gallery, but its using too much memory.

I wanted to implement Glide, but it wont work.

My public ImageAdapter class:

public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Activity context;

    public ImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public int getCount() { return count; }

    public Object getItem(int position) {
        return position;
    }

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


    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.galleryitem, null);
            holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
            holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
            holder.checkbox.setId(position);
            holder.imageview.setId(position);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        View.OnClickListener clickListener = new View.OnClickListener() {


            public void onClick(View v) {
                int id = holder.checkbox.getId();

                if (thumbnailsselection[id]) {
                    holder.checkbox.setChecked(false);
                    thumbnailsselection[id] = false;
                } else {
                    holder.checkbox.setChecked(true);
                    thumbnailsselection[id] = true;
                }
            }

        };

        holder.checkbox.setOnClickListener(clickListener);
        holder.imageview.setOnClickListener(clickListener);

        holder.imageview.setOnLongClickListener(new View.OnLongClickListener() {

            public boolean onLongClick(View v) {
                // TODO Auto-generated method stub
                int id = v.getId();
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*");
                startActivity(intent);
                return true;
            }

        });
        holder.imageview.setImageBitmap(thumbnails[position]);
        holder.checkbox.setChecked(thumbnailsselection[position]);
        holder.id = position;


        Glide.with(context).load(holder.imageview)
                .placeholder(R.drawable.logo).centerCrop()
                .into((ImageView) convertView);

        return convertView;
    }
}

class ViewHolder {
    ImageView imageview;
    CheckBox checkbox;
    int id;
}

oncreate method:

 GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
 imagegrid.setAdapter(new ImageAdapter());

Logcat:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.app.Activity.isDestroyed()' on a null object reference
at com.bumptech.glide.manager.RequestManagerRetriever.assertNotDestroyed(RequestManagerRetriever.java:133)
at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:125)
at com.bumptech.glide.Glide.with(Glide.java:641)
at com.example.example.photonet.Gallery$ImageAdapter.getView(Gallery.java:170)

I had to cast the convertView into Imageview to AndroidStudio accept it.

Without the Glide part it works well. So the problem is in that part.

Sam Judd
  • 7,317
  • 1
  • 38
  • 38
Janos
  • 197
  • 4
  • 16
  • check [this](http://stackoverflow.com/questions/39093730/you-cannot-start-a-load-for-a-destroyed-activity-in-relativelayout-image-using-g). same issue happened to me.Use picasso – Stephen Sep 02 '16 at 08:07

3 Answers3

0

Change

Note: For use context. you need to context like Sohail Zahid answer here

Glide.with(context).load("Url")
            .placeholder(R.drawable.logo).centerCrop()
            .into(holder.imageview);

for Load sdcard you need to follow this

 String fileName = "1.jpg";
 String completePath = Environment.getExternalStorageDirectory() + "/" + fileName;

File file = new File(completePath);
Uri imageUri = Uri.fromFile(file);

Glide.with(this)
        .load(imageUri)
                .into(holder.imageview);
Arjun saini
  • 4,223
  • 3
  • 23
  • 51
0

Make some modification as below pass context in constructor ImageAdapter(context) as parameter while setting adatper.

imagegrid.setAdapter(new ImageAdapter(ActivityName.this));

public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context context;

    public ImageAdapter(Context context) {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = context;
    }

Update: (ImageView) convertView to holder.imageview

 Glide.with(context).load(holder.imageview)
                .placeholder(R.drawable.logo).centerCrop()
                .into(holder.imageview);
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
  • java.lang.IllegalArgumentException: Unknown type class android.support.v7.widget.AppCompatImageView. You must provide a Model of a type for which there is a registered ModelLoader, if you are using a custom model, you must first call Glide#register with a ModelLoaderFactory for your custom model class – Janos Sep 02 '16 at 07:41
0
GalleryImages image = images.get(position);
//image.getMedium() == this is your image url
//holder.thumbnail = this is your imageview
    Glide.with(mContext).load(image.getMedium())

            .thumbnail(0.5f)
            .crossFade()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(holder.thumbnail);
Sathish Gadde
  • 1,453
  • 16
  • 28