I have an image (1.84MB) to load and I want to load a smaller version of it to gain time, I also don't want to store a smaller version of it. For info, these files are located in a removable SD card, and I need to load a lot, thats why I want to optimize it, as it takes too much time for the moment...
I red the load large bitmaps documentation. I previously used Glide to load the full image.
After some test, I get this statistics: Glide load (1.84MB): 244'361'667 ns Bitmap load (scale 1): 370'811'511 ns Bitmap load (scale 4): 324'403'386 ns Bitmap load (scale 8): 269'223'073 ns Thumbnail load: 245'250'729 ns
Which means I get a slightly longer time with Bitmap while loading a 8 times smaller image, and I don't get why.
Here is my code:
load with glide:
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);
}
load with bitmap:
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);
}
}
I also tried to load thumbnails:
private void loadThumbnail() {
Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile("filepath"),
816, 612);
ImageView img = new ImageView(this);
img.setImageBitmap(ThumbImage);
resultList.addView(img);
}
And here is the test code if you want:
long t0 = System.nanoTime();
scaledLoad(c, 1);
// or
loadGlide(c);
long t1 = System.nanoTime();
t1 = (t1-t0);
Log.d("test", "t1:" + t1);
If you can answer me or give me any other alternative to load underscaled images from SD card FAST, you are welcome. Thanks in advance.