bumptech / bumptech/glide

Glide Optimisation

Open
#4,629 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
35k
Forks
6.2k
Avg merge
1d 11h
Merged PRs (30d)
8

Description

**Glide Version**:
**Integration libraries**:
```
api 'com.github.bumptech.glide:glide:4.11.0'
api 'com.github.bumptech.glide:okhttp3-integration:4.11.0'
kapt 'com.github.bumptech.glide:compiler:4.11.0'
```

**Issue details / Repro steps / Use case background**:
I am getting an unidentified error that Images are not loading for some cases.
I checked in Mi/Xiaomi Device this is happening while not on Samsung.

Recent optimization that we shared earlier is
1. Reduced Disk Cache Size
2. Using DiskCache Strategy to Resource

Will any of this affect image loading somehow?

```
@GlideModule
class MainAppGlideModule : AppGlideModule() {

override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
registry.prepend(String::class.java, Bitmap::class.java, VideoThumbnailModelLoaderFactory())
val builder = OkHttpClient.Builder()
StethoUtils.addNetworkInterceptor(builder)
registry.replace(
GlideUrl::class.java,
InputStream::class.java,
OkHttpUrlLoader.Factory(builder.build())
)
}

override fun applyOptions(context: Context, builder: GlideBuilder) {
super.applyOptions(context, builder)
if (RemoteConfigUtils.enableGlideOptimisation()) {
val diskCacheSizeBytes = 1024 * 1024 * 150 // 150 MB
builder.setDiskCache(
InternalCacheDiskCacheFactory(
context,
diskCacheSizeBytes.toLong()
)
)
}
}
}
```

` .diskCacheStrategy(DiskCacheStrategy.RESOURCE)`

**Observations**
There is no network call performed to fetch the image as we have integrated Glide Image with Okhttp to review the images loading.

**Removing VideoThumbnailModelLoaderFactory seems to be working.**
```
public class VideoThumbnailModelLoaderFactory implements ModelLoaderFactory {

@NotNull
@Override
public ModelLoader build(@NotNull MultiModelLoaderFactory unused) {
return new VideoThumbnailModelLoader();
}

@Override
public void teardown() {
// Do nothing.
}
}
```

```
public final class VideoThumbnailModelLoader implements ModelLoader {

@Nullable
@Override
public LoadData buildLoadData(@NotNull String model, int width, int height, @NotNull Options options) {
return new LoadData<>(new ObjectKey(model), new VideoThumbnailDataFetcher(model));
}

@Override
public boolean handles(String model) {
return model.contains(".mp4") && !model.contains(".png") && !model.contains(".jpg") && !model.contains(".jpeg");
}
}
```

```
public class VideoThumbnailDataFetcher implements DataFetcher {

private final String model;
private MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();

VideoThumbnailDataFetcher(String model) {
this.model = model;
}

@Override
public void loadData(@NonNull Priority priority, @NonNull DataCallback callback) {
Bitmap bitmap = null;
try {
mediaMetadataRetriever.setDataSource(model, new HashMap<>());
bitmap = mediaMetadataRetriever.getFrameAtTime();
callback.onDataReady(bitmap);
} catch (Exception e) {
e.printStackTrace();
} finally {
release();
}
}

@Override
public void cleanup() {
release();
}

@Override
public void cancel() {
//empty
}

private void release() {
if (mediaMetadataRetriever != null) {
try {
mediaMetadataRetriever.release();
} catch (Exception e) {
e.printStackTrace();
}
mediaMetadataRetriever = null;
}
}

@NonNull
@Override
public Class getDataClass() {
return Bitmap.class;
}

@NonNull
@Override
public DataSource getDataSource() {
return DataSource.LOCAL;
}
}
```

Does VideoThumbnailDataFetcher download the mp4 video fully or only the single frame? Can you suggest us some optimization, if any?

This Looks related -> https://github.com/bumptech/glide/issues/4021

[Glide Logs.pdf](https://github.com/bumptech/glide/files/7124237/Glide.Logs.pdf)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.