Ensure bitmaps are not recycled when getting them from the cache.
This commit is contained in:
parent
5fbf0a98b9
commit
61cd9767c8
1 changed files with 11 additions and 6 deletions
|
@ -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);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue