1

I'm writing app with ActionBar Tabs and ViewPager. I have two tabs and two pages. First page is just Fragmet and second is ListFragment.

In first page i'm inverting infromations about plat like name, interval between water, photo etc. I can add default photo (from resources) and take photo of plant. Then i save photo's data in database like String in two ways: id of image from resources or path to saved photo.

In second page (ListFragment) is ListView with custom adapter and there is the problem. One row of list contains two TextViews, CheckBox and ImageView.

In method getView of my custom adapter every line works fine, but decoding file to bitmap takes lot of time and it makes my app alowly, even after decoding all photos.

It's my code to set up an image in each list row:

private void setUpImage(int position, ImageView photo)
    {
        if(plants.get(position).getPhoto().contains("jpg"))
        {
            BitmapFactory.Options options = new BitmapFactory.Options();

            options.inSampleSize = 10;

            Bitmap bitmap = BitmapFactory.decodeFile(plants.get(position).getPhoto(), options);

            photo.setImageBitmap(bitmap);
        }
        else
        {
            photo.setImageResource(Integer.parseInt(plants.get(position).getPhoto()));
        }
    }

Do anyone know how to improve speed of decoding and seting image in each list row of my ListView?

user2511941
  • 161
  • 1
  • 5
  • 14

1 Answers1

3

First of all! Setting options.inSampleSize = 10; is much greater. Normally, inSampleSize is provided in power of 2. And setting it equal to 2 or 4 is ok.

You can decode your file in background thread or you can use Lazy Loading techniques.

You can get Lazy Loading code here. See this answer.

Community
  • 1
  • 1
Faizan Mubasher
  • 4,427
  • 11
  • 45
  • 81
  • Ok i used Handler with Thread where i'm decoding each file, thanks. I have one more question. In first page, when i click on EditText and it takes a focus, keyboard which appear to write some text into that EditText, take space of first page's layuot. When keyboard disappears, layout take all available space like have to be. Can i do something to that keyboard don't take any space when it appears? – user2511941 Jan 30 '14 at 19:09
  • Well! I haven't came across such requirement yet! But you can see the Android docs. http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft – Faizan Mubasher Jan 31 '14 at 04:36
  • Thank you for mentioning: inSampleSize. This is helped me to resolve perfomance issues with bitmaps in my App – Vitaly Jul 12 '21 at 09:45