【发布时间】:2020-05-04 22:25:16
【问题描述】:
我有一张图片 (1.84MB) 需要加载,我想加载它的较小版本以节省时间,我也不想存储它的较小版本。对于信息,这些文件位于可移动的 SD 卡中,我需要加载很多,这就是为什么我要优化它,因为目前需要太多时间......
我红色了load large bitmaps 文档。我之前使用 Glide 来加载完整的图像。
经过一些测试,我得到了以下统计数据: 滑动负载(1.84MB):244'361'667 ns 位图加载(比例 1):370'811'511 ns 位图加载(比例 4):324'403'386 ns 位图加载(比例 8):269'223'073 ns 缩略图加载:245'250'729 ns
这意味着我在加载小 8 倍的图像时使用 Bitmap 的时间稍长,但我不明白为什么。
这是我的代码:
滑动加载:
private void loadGlide(Car c) {
DocumentFile makerDir = contentDirectory.findFile(getMakerById(c.getMakerId()).getName());
DocumentFile carPhoto = makerDir.findFile(c.getFilename());
if(carPhoto == null) {
Log.d("test", c.getFilename() + " problem");
} else {
Log.d("test", carPhoto.exists() + "");
}
ImageView img = new ImageView(this);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goToEdit(c.getId());
}
});
// standard loading
Glide.with(img.getContext()).load(carPhoto.getUri()).into(img);
resultList.addView(img);
}
使用位图加载:
private void scaledLoad(Car c, int scale) {
DocumentFile makerDir = contentDirectory.findFile(getMakerById(c.getMakerId()).getName());
DocumentFile carPhoto = makerDir.findFile(c.getFilename());
Bitmap b = null;
//Decode image size
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = scale;
// convert DocumentFile to File
File image = new File("image_path");
FileInputStream fis = null;
try {
fis = new FileInputStream(image);
b = BitmapFactory.decodeStream(fis, null, options);
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(b != null) {
ImageView img = new ImageView(this);
img.setImageBitmap(b);
resultList.addView(img);
}
}
我也尝试加载thumbnails:
private void loadThumbnail() {
Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile("filepath"),
816, 612);
ImageView img = new ImageView(this);
img.setImageBitmap(ThumbImage);
resultList.addView(img);
}
如果需要,这里是测试代码:
long t0 = System.nanoTime();
scaledLoad(c, 1);
// or
loadGlide(c);
long t1 = System.nanoTime();
t1 = (t1-t0);
Log.d("test", "t1:" + t1);
如果您能回答我或给我任何其他替代方法以从 SD 卡 FAST 加载缩小的图像,欢迎您。提前致谢。
【问题讨论】:
-
您好,“缩小”是什么意思?
标签: java android android-glide bitmapfactory