0

I am using Gridview in my App to show Images from a folder on Sdcard... My issue is the Scrolling in the GridView, it is not as smooth as the Gallery App. Here's the Adapter code -

public class GridViewAdapter extends BaseAdapter {

    // Declare variables
    private Activity activity;
    private String[] filepath;
    private String[] filename;

    private static LayoutInflater inflater = null;

    public GridViewAdapter(Activity a, String[] fpath, String[] fname) {
        activity = a;
        filepath = fpath;
        filename = fname;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public int getCount() {
        return filepath.length;

    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)

            vi = inflater.inflate(R.layout.gridview_item, null);

        // Locate the ImageView in gridview_item.xml
        ImageView img = (ImageView) vi.findViewById(R.id.image);


        // Set file name to the TextView followed by the position
        TextView txt = (TextView) vi.findViewById(R.id.name);

        // Decode the filepath with BitmapFactory followed by the position
        Bitmap bmp = BitmapFactory.decodeFile(filepath[position]);

        // Set the decoded bitmap into ImageView
        img.setImageBitmap(bmp);
       txt.setText(filename[position]);
        return vi;
    }
}

How to solve this Issue and make the scrolling smooth?

Darshan
  • 4,020
  • 2
  • 18
  • 49

3 Answers3

1

You need to execute BitmapFactory.decodeFile in a separate thread, like this tutorial: https://developer.android.com/training/displaying-bitmaps/process-bitmap.html

I suggest you use a cache, to not reload the images every time the user scroll: https://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

Emerson Dallagnol
  • 1,269
  • 1
  • 12
  • 21
1

You can do this.

  1. Implemented Asynchronous image loading so that every image get loaded asynchronously
  2. Make use of third party ImageLoaders and load images in Thumbnails size. For.ex UniversalImageLoader. check this more details
  3. You can make use of RecyclerView with GridLayoutManager instead of GridView, it is more optimized control. for ex:http://www.android-examples.com/android-recyclerview-with-gridview-gridlayoutmanager/

additional reference:http://www.technotalkative.com/android-select-multiple-photos-from-gallery/

Community
  • 1
  • 1
Gopal
  • 1,734
  • 1
  • 14
  • 34
1

BitmapFactory.decodeFile is heavy operation to perform on ui thread again and again. you have use some cache library.

You can use some image loading library like Universal image loader , Glide or picasso .

Please try this using a holder.

class Holder{
ImageView img ;
TextView txt;
}


public View getView(int position, View convertView, ViewGroup parent) {      
    Holder holder;
    if (convertView == null)
 {
     convertView = inflater.inflate(R.layout.gridview_item, null);
       holder=new Holder();

    holder.img = (ImageView) convertView.findViewById(R.id.image);

   holder.txt = (TextView) convertView.findViewById(R.id.name);

     convertView.setTag(holder)
    }
  else
{
  holder=(Holder)convertView.getTag();
 }


   Picasso.with(activity) .load(filepath[position]) .into(holder.img);
   txt.setText(filename[position]);
    return convertView;
}
Ankur1994a
  • 2,112
  • 2
  • 13
  • 18
  • I used Picasso but it doesn't show images..my Imageviews are blank.. Picasso.with(activity) .load(filepath[position]) .into(img); – Darshan Jan 20 '17 at 13:27
  • try to implement this using in a way as i edit my answer. – Ankur1994a Jan 23 '17 at 10:05
  • it's your choice to use glide or picasso. but for better result picasso is good. – Ankur1994a Jan 30 '17 at 10:05
  • Picasso couldn't load the Images... It was completely blank... So how can it be good as compared to Glide? :) – Darshan Jan 30 '17 at 12:01
  • i can't say what would be issue that picasso is not showing images where glide is showing. but as compare in all manner picasso is good. Anyways i think your problem is solved using glide,so you can use glide. – Ankur1994a Jan 30 '17 at 12:52