1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.androidx.graphics; 18 19 import android.graphics.Bitmap; 20 import android.provider.MediaStore; 21 import android.widget.ImageView; 22 23 import androidx.collection.LruCache; 24 import androidx.core.graphics.BitmapCompat; 25 26 /** 27 * A very naive lazily implemented image loader. Do not use this in production code. 28 */ 29 class ImageLoader { 30 31 /** 32 * A LruCache used to store images which has a maximum size of 10% of the maximum heap size. 33 */ 34 private static final long CACHE_SIZE = Runtime.getRuntime().maxMemory() / 10; 35 private static final BitmapCache CACHE = new BitmapCache( 36 CACHE_SIZE > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) CACHE_SIZE); 37 ImageLoader()38 private ImageLoader() { 39 } 40 41 interface Listener { onImageLoaded(Bitmap bitmap)42 void onImageLoaded(Bitmap bitmap); 43 } 44 45 @SuppressWarnings("deprecation") /* AsyncTask */ loadMediaStoreThumbnail(final ImageView imageView, final long id, final Listener listener)46 static void loadMediaStoreThumbnail(final ImageView imageView, 47 final long id, 48 final Listener listener) { 49 50 final Bitmap cachedValue = CACHE.get(id); 51 if (cachedValue != null) { 52 // If the image is already in the cache, display the image, 53 // call the listener now and return 54 imageView.setImageBitmap(cachedValue); 55 if (listener != null) { 56 listener.onImageLoaded(cachedValue); 57 } 58 return; 59 } 60 61 new android.os.AsyncTask<Void, Void, Bitmap>() { 62 @Override 63 protected Bitmap doInBackground(Void... params) { 64 return MediaStore.Images.Thumbnails.getThumbnail( 65 imageView.getContext().getContentResolver(), 66 id, 67 MediaStore.Images.Thumbnails.MINI_KIND, 68 null); 69 } 70 71 @Override 72 protected void onPostExecute(Bitmap bitmap) { 73 imageView.setImageBitmap(bitmap); 74 75 if (bitmap != null) { 76 // Add the image to the memory cache first 77 CACHE.put(id, bitmap); 78 79 if (listener != null) { 80 listener.onImageLoaded(bitmap); 81 } 82 } 83 } 84 }.executeOnExecutor(android.os.AsyncTask.THREAD_POOL_EXECUTOR); 85 } 86 87 /** 88 * A simple cache implementation for {@link android.graphics.Bitmap} instances which uses 89 * {@link androidx.collection.LruCache}. 90 */ 91 private static class BitmapCache extends LruCache<Long, Bitmap> { BitmapCache(int maxSize)92 BitmapCache(int maxSize) { 93 super(maxSize); 94 } 95 96 @Override sizeOf(Long key, Bitmap value)97 protected int sizeOf(Long key, Bitmap value) { 98 return BitmapCompat.getAllocationByteCount(value); 99 } 100 } 101 102 } 103