【问题标题】:How to convert list of Image file paths into list of bitmaps quickly?如何快速将图像文件路径列表转换为位图列表?
【发布时间】:2017-08-03 20:51:11
【问题描述】:
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);

处理需要很长时间。有没有办法缩短流程?

【问题讨论】:

    标签: android image arraylist bitmap


    【解决方案1】:

    有没有办法缩短流程?

    使用您当前的算法,拥有更小的图像列表。

    您可以通过以下方式为自己节省大量时间和大量内存:

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

    • 使用带有BitmapFactory.Options 的两参数decodeFile(),并提供inSampleSize 2(1/4)或3(1/8)

    一般来说,一次加载一大堆图像不是一个好主意,因此我强烈建议您找到一种方法来加载图像,当且仅当它们需要时。例如,如果用户在该目录中有数百张高分辨率照片,您将因OutOfMemoryError 而崩溃,因为您没有足够的堆空间来容纳数百张图像。

    【讨论】:

    • 我需要提供至少 10 张图片,不能比这个更小。
    • 尝试了原始图像的1/8,它正在快速将图像绑定到recylerview。但是将文件对象转换为位图并将它们存储在 arraylist 中仍然需要时间。
    • @ShubhanshJaiswal:如果您使用的是RecyclerView,请使用图像加载库(Glide、Picasso 等)根据需要加载图像,而不是加载他们在前面。
    • 我需要做的是从相机文件夹中只绑定10张最新的图像,可以通过glide或picassso完成
    • @ShubhanshJaiswal:图像加载库只加载您告诉它加载的内容。您的工作是将加载限制为您想要的特定 10 张图像。
    猜你喜欢
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 2011-03-18
    • 1970-01-01
    相关资源
    最近更新 更多