0

I am trying to load images from Internal storage of phone in a GridView. GridView is in a fragment. I have a class that stores the files paths to all images. I initialize the class's data once so that I don't have to it again and again. And then I set the adapter to GridView using that class.

This is the function of my data class.

public static void initialize(Cursor cursor) {
    int count = cursor.getCount();
    allPaths = new String[count];

    for (int i = 0; i < count; i++) {
        cursor.moveToPosition(i);
        int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);

        allPaths[i] = cursor.getString(dataColumnIndex);
    }
    cursor.close();
}

This is the adapter of my Grid:

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView mImageView;

    if (convertView == null) {
        mImageView = new ImageView(mContext);
        mImageView.setLayoutParams(new GridView.LayoutParams(250, 250));
        mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    } else {
        mImageView = (ImageView) convertView;
    }
    File f = new File(AllScreenshots.get(position));
    Picasso.get().load(f).into(mImageView);
    return mImageView;
}

The loading of images, scrolling of grid view and app becomes so slow. How can I make the loading, scrolling and app smooth and fast again?

Umer Maqsood
  • 117
  • 1
  • 11

1 Answers1

1

I used RecyclerView and Glide library instead of GridView and Picasso. In my RecyclerView adapter's onBindViewHolder method I used glide's method

Glide.with(context).load(AllScreenshots.get(position)).thumbnail(0.5f).diskCacheStrategy(DiskCacheStrategy.ALL).into(holder.img);

It solved the issue for me.

Umer Maqsood
  • 117
  • 1
  • 11
  • Thank you very much ! I tried Everything!! including Picasso library but nothing worked, I was changing my code for hours and you solved it with one line,Thanks! – HoLoGram Oct 08 '21 at 02:33