简体   繁体   中英

How to handle large amount of pictures. which thumbnails sould be shown in android app?

I have a problem in my android application. I use an application which make pictures with the build in camera of a phone/tablet.These pictures are sometimes quite large in size because of the high resolution of the camera.

Now i load the application and create a listView, where a small thumbnail of the picture is on the left side and on the right is some text.

I create the pictures in this way:

Bitmap a = BitmapFactory.decodeFile(url);
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
         (float) 66.0, context.getResources().getDisplayMetrics());
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
         (float) 48.0, context.getResources().getDisplayMetrics());
Bitmap b = Bitmap.createScaledBitmap(a, width, height, false);

The problem is that this step take quite a long time.

Is there any way to make this faster?

There is no way to make it faster, but you should not block the UI thread. See

  1. AsyncTask (and many related questions like Using AsyncTask to load Images in ListView )
  2. LruCache

This is the solution I used taken from here: https://stackoverflow.com/a/5934983/1369222

It works great!

byte[] imageData = null;

        try     
        {

            final int THUMBNAIL_SIZE = 64;

            FileInputStream fis = new FileInputStream(fileName);
            Bitmap imageBitmap = BitmapFactory.decodeStream(fis);

            imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, false);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            imageData = baos.toByteArray();

        }
        catch(Exception ex) {

        }

Make sure you do this in an Async or a separate thread task so you don't block up the UI thread!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM