I've read this http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html but now I'm looking for an easy way of implementing the disk cache into my app. I've found that lib http://square.github.io/picasso/ but it isn't working. I always get a "NoClassDefFoundError". Do you know a library that could easily allow me to cache downloaded bitmaps? Thank you
Asked
Active
Viewed 4,472 times
0
-
1use universal image loader – Raghunandan Jul 04 '13 at 05:22
-
you can find the example http://stackoverflow.com/questions/16789676/caching-images-and-displaying/16978285#16978285 – Raghunandan Jul 04 '13 at 05:25
-
Thanks, I'm going to read it – Romain Pellerin Jul 04 '13 at 05:31
-
Could I simply implement this https://github.com/thest1/LazyList ? – Romain Pellerin Jul 04 '13 at 05:36
-
http://stackoverflow.com/a/32516598/1939564 – Muhammad Babar Oct 12 '15 at 10:04
4 Answers
1
Regarding the "NoClassDefFoundError", this may be fixed by:
- right click your project
- properties
- Java build path
- In the "Libraries" tab - add your lib
- In the "Order and Export" tab - make sure the lib you added is checked

dors
- 5,802
- 8
- 45
- 71
-
Gloooops, you fixed it! Thank you so much! What does the checkbox change? – Romain Pellerin Jul 04 '13 at 05:58
-
1I'm glad that helped :) This is one of eclipses more annoying bugs that I encountered :/ Sometimes adding a lib to the libs folder does not automatically adds it fully to the project's build process. That checkbox does that manually. – dors Jul 04 '13 at 06:00
0
You can try this code to cache image from url :
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import android.graphics.Bitmap;
public class MemoryCache {
String TAG = "MemoryCache";
private Map<String, Bitmap> cache=Collections.synchronizedMap(
new LinkedHashMap<String, Bitmap>(10,1.5f,true));//Last argument true for LRU ordering
private long size=0;//current allocated size
private long limit=1000000;//max memory in bytes
public MemoryCache(){
//use 25% of available heap size
setLimit(Runtime.getRuntime().maxMemory()/4);
}
public void setLimit(long new_limit){
limit=new_limit;
}
public Bitmap get(String id){
try{
if(!cache.containsKey(id))
return null;
//NullPointerException sometimes happen here http://code.google.com/p/osmdroid/issues/detail?id=78
return cache.get(id);
}catch(NullPointerException ex){
return null;
}
}
public void put(String id, Bitmap bitmap){
try{
if(cache.containsKey(id))
size-=getSizeInBytes(cache.get(id));
cache.put(id, bitmap);
size+=getSizeInBytes(bitmap);
checkSize();
}catch(Throwable th){
th.printStackTrace();
}
}
private void checkSize() {
if(size>limit){
Iterator<Entry<String, Bitmap>> iter=cache.entrySet().iterator();//least recently accessed item will be the first one iterated
while(iter.hasNext()){
Entry<String, Bitmap> entry=iter.next();
size-=getSizeInBytes(entry.getValue());
iter.remove();
if(size<=limit)
break;
}
}
}
public void clear() {
cache.clear();
}
long getSizeInBytes(Bitmap bitmap) {
if(bitmap==null)
return 0;
return bitmap.getRowBytes() * bitmap.getHeight();
}
}
usage is :
memoryCache.put(photoToLoad.url, bmp);
and to get:
memoryCache.get(url);
if you have some question feel free to ask in the comment!

Niko Adrianus Yuwono
- 11,012
- 8
- 42
- 64
-
1Thank you, but this is using memory cache right? I want to use disk cache. – Romain Pellerin Jul 04 '13 at 05:39
0
"Solution for android Stdio."
To use picasso lib you have to make an entry in
build.gradle
dependencies section add
compile 'com.squareup.picasso:picasso:2.5.2'
"NoClassDefFoundError" will not come and now you can use picasso to cache images

Sunil Agarwal
- 91
- 7
0
I just created a new cache library VIF to answer this need:
Here is how to use it
@Override
public void run() {
appCache = new DiskCache(this, "app_cache", 1024 * 1024 * 20);// 20 MB cache
downloadImage(url);
loadImage(imageView, url);
}
private void downloadImage(String imageUrl) {
URL url = new URL(imageUrl);
URLConnection connection = url.openConnection();
connection.connect();
appCache.put(imageUrl, connection.getInputStream());
}
private void loadImage(final ImageView imageView, String imageUrl) {
appCache.getAsObject(MY_IMAGE, new DiskCache.ParserCallback<Bitmap>() {
@Override
public Bitmap parse(@NonNull File file) throws Exception {
return BitmapFactory.decodeFile(file.getAbsolutePath());
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onResult(@Nullable Bitmap result) {
imageView.setImageBitmap(result);
}
});
}

Ilya Gazman
- 31,250
- 24
- 137
- 216