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/