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

标签 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);

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

最佳答案

Is there a way to shorten the process?

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

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

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

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

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

关于android - 如何快速将图像文件路径列表转换为位图列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45494283/

相关文章:

android - Admob SMART_BANNER 和 BANNER 之间的区别

Android:如何从收件箱获取短信并删除特定短信

image - 如何使用spring mvc显示mysql数据库中的图像

java - 使用 ArrayList 作为类中的静态属性?

java - 从java中的arrayList打印数组

Android FrameLayout 和 TextView - 为什么 LayoutParams 在 setGravity() 不起作用的地方起作用?

android - 更改单选按钮的圆圈颜色

r - 检测图像中的颜色

asp.net - 如何将 'System.Drawing.Image' 添加到 'System.Web.UI.WebControls.Image'

java - 在android(客户端)中创建ArrayList以从Mysql(服务器)检索信息的步骤是什么?