Reputation: 8134
I was just reading tutorial of caching bitmaps from url at developer.android.com
refering to code
private LruCache<String, Bitmap> mMemoryCache;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Get memory class of this device, exceeding this amount will throw an
// OutOfMemory exception.
final int memClass = ((ActivityManager) context.getSystemService(
Context.ACTIVITY_SERVICE)).getMemoryClass();
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = 1024 * 1024 * memClass / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in bytes rather than number of items.
return bitmap.getByteCount();
}
};
...
}
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
public Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}
public void loadBitmap(int resId, ImageView imageView) {
final String imageKey = String.valueOf(resId);
final Bitmap bitmap = getBitmapFromMemCache(imageKey);
if (bitmap != null) {
mImageView.setImageBitmap(bitmap);
} else {
mImageView.setImageResource(R.drawable.image_placeholder);
BitmapWorkerTask task = new BitmapWorkerTask(mImageView);
task.execute(resId);
}
}
in activity cache is beign initialize and there are two methods
addBitmapToMemoryCache(String key, Bitmap bitmap)
getBitmapFromMemCache(String key)
here is the BitmapWorkerTask class
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
...
// Decode image in background.
@Override
protected Bitmap doInBackground(Integer... params) {
final Bitmap bitmap = decodeSampledBitmapFromResource(
getResources(), params[0], 100, 100));
addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
return bitmap;
}
...
}
now my question is that in BitmapWorkerTask class how come method addBitmapToMemoryCache(String.valueOf(params[0]), bitmap); is called with having any reference to the methods that are defined in Activty or might be in some other Class
Upvotes: 1
Views: 409
Reputation: 1730
For someone who still couldn't get the sample code work,
In order to get the code working, you should make BitmapWorkerTask class an inner class and then change its two static functions
public static int calculateInSampleSize()
public static Bitmap decodeSampledBitmapFromResource()
to normal (non static) methods
Upvotes: 0
Reputation: 928
The BitmapWorkerTask is probably an inner class within the activity, thus providing it access to the defined methods.
Upvotes: 2