如何快速将图像文件路径列表转换为位图列表?

时间:2017-08-03 20:51:11

标签: android image arraylist bitmap

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);
}

当我将文件对象转换为位图并重新缩放它们时:

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

处理需要花费大量时间。有没有办法缩短过程?

1 个答案:

答案 0 :(得分:0)

  

有没有办法缩短流程?

使用当前算法,可以使用较小的图像列表。

你可以通过以下方式节省很多时间和大量的记忆:

  • 从“原始图像的1/6”切换到“原始图像的1/4”或“原始图像的1/8”,然后

  • 使用带有decodeFile()的双参数BitmapFactory.Options,并提供inSampleSize为2(对于1/4)或3(对于1/8)< / p>

一般情况下,一次加载一大堆图像并不是一个好主意,因此我强烈建议您在需要时才能找到加载图像的方法。例如,如果用户在该目录中有数百张高分辨率照片,那么您将使用OutOfMemoryError崩溃,因为您没有足够的堆空间来容纳数百张图像。