Answer the question
In order to leave comments, you need to log in
Android: GridView and custom adapter - how to avoid brakes on a large amount of data when displaying?
There is a GridView and a custom ImageAdapter inherited from BaseAdapter. The idea is this: there is a folder with photos on an android device that is scanned and all the photos of this folder are displayed in the GridView.
I'm trying to do this: in the adapter's constructor, all photos are selected and absolute paths are stored in an array of strings. In its adapter, the View getView() method is overridden, in which an ImageView is dynamically created, into which a photo is inserted using the BitmapFactory.
So, on a large number of photos in a folder (hundred and a half or two), GridView is terribly slow. I suspect that this happens for two reasons:
1) Perhaps you need to shrink the Photo that is inserted into the ImageView.
2) Wrong in the adapter's getView() method to create every timean ImageView instance.
Are my guesses correct? how to correctly display a list of photos from a folder in a GridView?
Answer the question
In order to leave comments, you need to log in
1) Get rid of disk operations in getView(). Store images in the cache (LRU or any on WeakReference), if there is no image in the cache, then give a stub to getView, and load the image itself asynchronously using AsyncTask
2) Get rid of creating ImageView objects in getView if the second parameter is non-null. GridView has a built-in optimization that allows you not to create new instances of views, but to use old ones, showing them in a new place with new content.
Here is the adapter code:
package com.example.phocom;
import java.io.File;
import java.io.FilenameFilter;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class ImageAdapter extends BaseAdapter
{
private Context mContext;
private String[] data = {"1"};
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap btm;
ImageView view;
public ImageAdapter(Context сontext)
{
mContext = сontext;
options.inSampleSize = 2;
// получаю список фотографий в папке /DCIM/Camera на sd-карте
File rootsd = Environment.getExternalStorageDirectory();
File dcim = new File(rootsd.getAbsolutePath() + "/DCIM/Camera");
File[] imagelist = dcim.listFiles(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
return ((name.endsWith(".jpg"))||(name.endsWith(".png")));
}
});
// сохраняю пути к фотографиям в стринг массиве
if (imagelist != null)
{
data = new String[imagelist.length];
for (int i = 0; i < imagelist.length; i++)
{
data[i] = imagelist[i].getAbsolutePath();
}
}
}
public int getCount()
{
return data.length;
}
public Object getItem(int position)
{
return null;
}
public long getItemId(int position)
{
return 0;
}
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
view = new ImageView(mContext);
view.setLayoutParams(new GridView.LayoutParams(100, 100));
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else
{
view = (ImageView)convertView;
}
btm = BitmapFactory.decodeFile(data[position], options);
view.setImageBitmap(btm);
return view;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question