我正在使用此处找到的示例: https ://github.com/octo-online/robospice/wiki/Starter-Guide
并试图扩展它以支持带有缓存的位图请求。将 LogCat 设置为详细时,我看到指示位图已成功放入缓存的日志消息,但尝试在后续调用中从缓存中读回位图失败,并显示以下日志消息:
11-15 09:14:02.694: D//RequestRunner.java:102(5462): com.octo.android.robospice.persistence.exception.CacheLoadingException: org.codehaus.jackson.map.JsonMappingException: 找不到合适的构造函数type [simple type, class android.graphics.Bitmap]:不能从JSON对象实例化(需要添加/启用类型信息?)
这是来自 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;
}
这是调用 spiceManager 调用以从缓存或网络加载图像的代码:
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);
}
}
我需要工厂从缓存数据中重构位图吗?由于这不是 Json 对象,我需要做些什么来纠正使用 Jackson Json 映射的尝试?如果我添加/启用类型信息,Jackson 会做正确的事吗?我怎么做?
感谢您阅读到这里!
-埃里克