0

I am new to android. I have more than thousand images in a directory. I am showing images thumb in grid view. With following code It's works. but the problem is it takes 45 second to load the view. I need it as: Display grid with loader and load Images one by one. So user can not wait for last image to load.

The code is:

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

        LayoutInflater layoutInflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ListRowHolder listRowHolder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.ll_sponsor_list_item,
                    parent, false);
            listRowHolder = new ListRowHolder();
            listRowHolder.imgSponsor = (ImageView) convertView
                    .findViewById(R.id.imggrid_item_image);
            convertView.setTag(listRowHolder);

        } else {
            listRowHolder = (ListRowHolder) convertView.getTag();
        }
        try {

            listRowHolder.imgSponsor
                    .setImageBitmap(decodeSampledBitmapFromResource(
                            ImageName.get(position)));
        } catch (Exception e) {
            Toast.makeText(ctx, e + "", Toast.LENGTH_SHORT).show();
        }

        return convertView;
    }


    public static Bitmap decodeSampledBitmapFromResource(String fileName) {
        Bitmap picture = BitmapFactory.decodeFile(fileName);
        int width = picture.getWidth();
        int height = picture.getWidth();
        float aspectRatio = (float) width / (float) height;
        int newWidth = 98;
        int newHeight = (int) (98 / aspectRatio);
        return picture = Bitmap.createScaledBitmap(picture, newWidth,
                newHeight, true);
    }
Uttam Kadam
  • 458
  • 1
  • 7
  • 20

1 Answers1

1

The reason it is slow is because you are decoding the file AND generating the scaled bitmap on your UI thread, for many images. Because you are doing long operations, you will need to do lazy loading of your images.

The premise is similar to the solution in the link (you can use a Handler) but you will be decoding the file and scaling the Bitmaps instead of downloading the image there.

Community
  • 1
  • 1
Oleg Vaskevich
  • 12,444
  • 6
  • 63
  • 80