How to quickly convert a list of image file paths to a list of bitmaps?

ArrayList<String> imageFileList = new ArrayList<>();
ArrayList<RecentImagesModel> fileInfo = new ArrayList<>();

File targetDirector = new File(/storage/emulated/0/DCIM/Camera/);
if (targetDirector.listFiles() != null) {
    File[] files = targetDirector.listFiles();
    int i = files.length - 1;
    while (imageFileList.size() < 10) {
       File file = files[i];
       if (file.getAbsoluteFile().toString().trim().endsWith(".jpg")) {
          imageFileList.add(file.getAbsolutePath());
       } else if (file.getAbsoluteFile().toString().trim().endsWith(".png")) {
          imageFileList.add(file.getAbsolutePath());
       }
       i--;
    }
}

String file, filename;
Bitmap rawBmp, proBmp;
int length = imageFileList.size();

for (int i = 0; i < length; i++) {
   RecentImagesModel rim = new RecentImagesModel();
   file = imageFileList.get(i);
   rim.setFilepath(file);
   filename = file.substring(file.lastIndexOf("/") + 1);
   rim.setName(filename);
   rawBmp = BitmapFactory.decodeFile(file);
   proBmp = Bitmap.createScaledBitmap(rawBmp, rawBmp.getWidth() / 6, rawBmp.getHeight() / 6, false);
   rim.setBitmap(proBmp);
   fileInfo.add(rim);
}

      

When I convert file objects to bitmaps and scale them:

rawBmp = BitmapFactory.decodeFile(file);
proBmp = Bitmap.createScaledBitmap(rawBmp, rawBmp.getWidth() / 6, rawBmp.getHeight() / 6, false);

      

It takes a long time to process. Is there a way to shorten the process?

+3


source to share


1 answer


Is there a way to shorten the process?

With your current algorithm, having a smaller list of images.

You could save a lot of time and a lot of memory:



  • Switch from "1/6 of the original image" to "1/4 of the original image" or "1/8 of the original image", then

  • Use a two-parameter decodeFile()

    that accepts BitmapFactory.Options

    , and put inSampleSize

    in 2 (for 1/4) or 3 (for 1/8)

In general, loading a whole series of images at once is not a good idea, so I highly recommend that you find a way to load images when and only when you need them. For example, if a user has hundreds of high resolution photos in this directory, you will run into OutOfMemoryError

as you run out of heap space to store hundreds of images.

0


source







All Articles