当前位置: 动力学知识库 > 问答 > 编程问答 >

caching - How to correctly use com.octo.android.robospice to cache bitmaps

问题描述:

I'm using the example found here:

https://github.com/octo-online/robospice/wiki/Starter-Guide

and have attempted to extend it to support bitmap requests with caching. With LogCat set to verbose, I see log messages indicating that the bitmaps are being put into cache successfully, but attempts to read the bitmaps back from cache on subsequent calls are failing with the following log message:

11-15 09:14:02.694: D//RequestRunner.java:102(5462): com.octo.android.robospice.persistence.exception.CacheLoadingException: org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class android.graphics.Bitmap]: can not instantiate from JSON object (need to add/enable type information?)

Here's the relevant code from the JsonSpiceFactory:

@Override

public CacheManager createCacheManager(Application application) {

CacheManager cacheManager = new CacheManager();

try {

final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

final int cacheSize = maxMemory / 8;

JacksonObjectPersisterFactory jacksonObjectPersisterFactory = new JacksonObjectPersisterFactory(application);

cacheManager.addPersister( jacksonObjectPersisterFactory );

// Try to add support for bitmap caching here

InFileBitmapObjectPersister filePersister = new InFileBitmapObjectPersister(application);

LruCacheBitmapObjectPersister memoryPersister = new LruCacheBitmapObjectPersister(filePersister, cacheSize);

cacheManager.addPersister(memoryPersister);

} catch (CacheCreationException e) {

e.printStackTrace();

}

return cacheManager;

}

And here's the code that invokes the spiceManager call to load the image from cache or network:

public void getNetworkBitmap(String url, String cacheKey, GetBitmapListener listener) {

GetMerchantLogoRequest request = new GetMerchantLogoRequest(url);

spiceManager.execute(request, cacheKey, DurationInMillis.ONE_HOUR, new NetworkBitmapRequestListener(listener));

}

private class NetworkBitmapRequestListener implements RequestListener<Bitmap> {

private GetBitmapListener listener;

public NetworkBitmapRequestListener(GetBitmapListener listener) {

this.listener = listener;

}

@Override

public void onRequestFailure(SpiceException spiceException) {

listener.onImageRequestFailure(spiceException);

}

@Override

public void onRequestSuccess(Bitmap logo) {

listener.onImageRequestSuccess(logo);

}

}

Do I need a factory to reconstitute the bitmap from the cache data? Since this isn't a Json object, what do I need to do to correct the attempt to use Jackson Json mapping? Will Jackson do the right thing if I add/enable type information? How do I do that?

Thanks for reading this far!

-Eric

网友答案:

I found the answer reading through the CacheManager (link below) and stumbled over the line "All elements [persisters] ... are questioned in order" so I re-ordered the entries in the CreateCacheManager method and now it's working.

http://grepcode.com/file/repo1.maven.org/maven2/com.octo.android.robospice/robospice-cache/1.3.1/com/octo/android/robospice/persistence/CacheManager.java

网友答案:

you can use BitmapRequest, here is an example:

https://github.com/octo-online/robospice/blob/release/robospice-core-parent/robospice-core-test/src/main/java/com/octo/android/robospice/request/BitmapRequestTest.java?source=cc

分享给朋友:
您可能感兴趣的文章:
随机阅读: