@@ -129,12 +129,13 @@ private void init(ImageCacheParams cacheParams) {
129
129
LOGD (TAG , "Memory cache created (size = " + mCacheParams .memCacheSize + ")" );
130
130
mMemoryCache = new LruCache <String , Bitmap >(mCacheParams .memCacheSize ) {
131
131
/**
132
- * Measure item size in bytes rather than units which is more practical
132
+ * Measure item size in kilobytes rather than units which is more practical
133
133
* for a bitmap cache
134
134
*/
135
135
@ Override
136
136
protected int sizeOf (String key , Bitmap bitmap ) {
137
- return getBitmapSize (bitmap );
137
+ final int bitmapSize = getBitmapSize (bitmap ) / 1024 ;
138
+ return bitmapSize == 0 ? 1 : bitmapSize ;
138
139
}
139
140
};
140
141
}
@@ -362,40 +363,41 @@ public static class ImageCacheParams {
362
363
public boolean initDiskCacheOnCreate = DEFAULT_INIT_DISK_CACHE_ON_CREATE ;
363
364
364
365
public ImageCacheParams (Context context ) {
365
- init (context , getDiskCacheDir (context , DEFAULT_DISK_CACHE_DIR ));
366
+ init (getDiskCacheDir (context , DEFAULT_DISK_CACHE_DIR ));
366
367
}
367
368
368
369
public ImageCacheParams (Context context , String uniqueName ) {
369
- init (context , getDiskCacheDir (context , uniqueName ));
370
+ init (getDiskCacheDir (context , uniqueName ));
370
371
}
371
372
372
- public ImageCacheParams (Context context , File diskCacheDir ) {
373
- init (context , diskCacheDir );
373
+ public ImageCacheParams (File diskCacheDir ) {
374
+ init (diskCacheDir );
374
375
}
375
376
376
- private void init (Context context , File diskCacheDir ) {
377
- setMemCacheSizePercent (context , DEFAULT_MEM_CACHE_PERCENT );
377
+ private void init (File diskCacheDir ) {
378
+ setMemCacheSizePercent (DEFAULT_MEM_CACHE_PERCENT );
378
379
this .diskCacheDir = diskCacheDir ;
379
380
}
380
381
381
382
/**
382
- * Sets the memory cache size based on a percentage of the device memory class.
383
- * Eg. setting percent to 0.2 would set the memory cache to one fifth of the device memory
384
- * class. Throws {@link IllegalArgumentException} if percent is < 0.05 or > .8.
383
+ * Sets the memory cache size based on a percentage of the max available VM memory.
384
+ * Eg. setting percent to 0.2 would set the memory cache to one fifth of the avilable
385
+ * memory. Throws {@link IllegalArgumentException} if percent is < 0.05 or > .8.
386
+ * memCacheSize is stored in kilobytes instead of bytes as this will eventually be passed
387
+ * to construct a LruCache which takes an int in its constructor.
385
388
*
386
389
* This value should be chosen carefully based on a number of factors
387
390
* Refer to the corresponding Android Training class for more discussion:
388
391
* http://developer.android.com/training/displaying-bitmaps/
389
392
*
390
- * @param context Context to use to fetch memory class
391
393
* @param percent Percent of memory class to use to size memory cache
392
394
*/
393
- public void setMemCacheSizePercent (Context context , float percent ) {
395
+ public void setMemCacheSizePercent (float percent ) {
394
396
if (percent < 0.05f || percent > 0.8f ) {
395
397
throw new IllegalArgumentException ("setMemCacheSizePercent - percent must be "
396
398
+ "between 0.05 and 0.8 (inclusive)" );
397
399
}
398
- memCacheSize = Math .round (percent * getMemoryClass ( context ) * 1024 * 1024 );
400
+ memCacheSize = Math .round (percent * Runtime . getRuntime (). maxMemory () / 1024 );
399
401
}
400
402
401
403
private static int getMemoryClass (Context context ) {
0 commit comments