I want to build a Gallery App that takes a list of photos from my folder and display it in one page.
I reduced the resolution of the image, so that the Gallery will load faster, but still it is taking so much time to load.
I think the problem is every file is loaded each time the app is opened. How can I solve this?
I used Gridview in the app.
My code:
final GridView[] grid = new GridView[1];
final File[] files = fileOps.getGoodFiles();
//this will return the array of files (images) to be displayed
final CustomGrid adapter = new CustomGrid(GeoGallery.this,files);
grid[0] =(GridView)findViewById(R.id.grid);
grid[0].setAdapter(adapter);
In my CustomGridView:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
grid = inflater.inflate(R.layout.grid_single, null);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
Bitmap bitmap = BitmapFactory.decodeFile(files[position].getPath());
bitmap = Bitmap.createScaledBitmap(bitmap,120,120*bitmap.getHeight()/bitmap.getWidth(),false);
imageView.setImageBitmap(bitmap);
return grid;
}
How can I make it load faster?
1) You should use a GridLayoutManager with RecyclerView. Here you can get some help.
2) If i am not wrong you are loading full images and then compressing it. You should use BitmapFactory.Options to load compressed image.
3) Operations like loading and manipulating images are very expensive. So put them into a thread in your onBindViewHolder Method
It will enhance the performance of your gallery.