如何快速将图像文件路径列表转换为位图列表? [英] How to convert list of Image file paths into list of bitmaps quickly?
问题描述
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 am converting the file objects to bitmaps and rescaling them:
rawBmp = BitmapFactory.decodeFile(file);
proBmp = Bitmap.createScaledBitmap(rawBmp, rawBmp.getWidth() / 6, rawBmp.getHeight() / 6, false);
处理需要很多时间.有没有办法缩短流程?
It takes a lot of time in processing. Is there a way to shorten the process?
推荐答案
有没有一种缩短流程的方法?
Is there a way to shorten the process?
使用您当前的算法,可以减少图像列表.
With your current algorithm, have a smaller list of images.
您可以通过以下方式节省大量时间和内存:
You could save yourself a lot of time and a lot of memory by:
-
从原始图像的1/6"切换到原始图像的1/4"或原始图像的1/8",然后
Switch from "1/6th of the original image" to either "1/4th of the original image" or "1/8th of the original image", then
使用两个参数 decodeFile()
,该参数采用 BitmapFactory.Options
,并提供2的 inSampleSize
(对于1/4th)或3(for 1/8th)
Use the two-parameter decodeFile()
that takes a BitmapFactory.Options
, and supply an inSampleSize
of 2 (for 1/4th) or 3 (for 1/8th)
通常,一次加载一大堆图像不是一个好主意,因此,我强烈建议您找到一种仅在需要时才加载图像的方法.例如,如果用户在该目录中有数百张高分辨率照片,则您将因 OutOfMemoryError
而崩溃,因为您没有足够的堆空间来容纳数百张图像.
In general, loading a whole bunch of images at once is not a good idea, and so I strongly encourage you to find a way to load images if and only if they are needed. For example, if the user has hundreds high-resolution photos in that directory, you will crash with an OutOfMemoryError
, as you do not have enough heap space for holding hundreds of images.
这篇关于如何快速将图像文件路径列表转换为位图列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!