Android Gallery App Loading Very Slow

2019-07-10 14:48发布

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条回答
smile是对你的礼貌
2楼-- · 2019-07-10 15:33

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.

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap b = BitmapFactory.decodeFile(filepath[position], options);

3) Operations like loading and manipulating images are very expensive. So put them into a thread in your onBindViewHolder Method

final int pos = position;
Thread thread = new Thread() {
     @Override
     public void run() {
          try {
               BitmapFactory.Options options = new BitmapFactory.Options();
               options.inSampleSize = 4;
               Bitmap b = BitmapFactory.decodeFile(filepath[pos], options);

               holder.imageView.setImageBitmap(b)

           } catch (Exception e)
       }
   }
};

It will enhance the performance of your gallery.

查看更多
登录 后发表回答