Ensure bitmaps are not recycled when getting them from the cache.

This commit is contained in:
Alex Hart 2023-07-10 17:08:27 -03:00 committed by GitHub
parent 5fbf0a98b9
commit 61cd9767c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -13,6 +13,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
@ -64,14 +65,16 @@ final class UriChatWallpaper implements ChatWallpaper, Parcelable {
@Override
public void loadInto(@NonNull ImageView imageView) {
Bitmap cached = CACHE.get(uri);
if (cached != null) {
if (cached != null && !cached.isRecycled()) {
Log.d(TAG, "Using cached value.");
imageView.setImageBitmap(CACHE.get(uri));
imageView.setImageBitmap(cached);
} else {
Log.d(TAG, "Not in cache. Fetching using Glide.");
GlideApp.with(imageView)
Log.d(TAG, "Not in cache or recycled. Fetching using Glide.");
GlideApp.with(imageView.getContext().getApplicationContext())
.asBitmap()
.load(new DecryptableStreamUriLoader.DecryptableUri(uri))
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.addListener(new RequestListener<>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
@ -94,16 +97,18 @@ final class UriChatWallpaper implements ChatWallpaper, Parcelable {
@Override
public boolean prefetch(@NonNull Context context, long maxWaitTime) {
Bitmap cached = CACHE.get(uri);
if (cached != null) {
if (cached != null && !cached.isRecycled()) {
Log.d(TAG, "Already cached, skipping prefetch.");
return true;
}
long startTime = System.currentTimeMillis();
try {
Bitmap bitmap = GlideApp.with(context)
Bitmap bitmap = GlideApp.with(context.getApplicationContext())
.asBitmap()
.load(new DecryptableStreamUriLoader.DecryptableUri(uri))
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.submit()
.get(maxWaitTime, TimeUnit.MILLISECONDS);