2

我正在使用此处找到的示例: 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 会做正确的事吗?我怎么做?

感谢您阅读到这里!

-埃里克

4

2 回答 2

2

我通过 CacheManager(下面的链接)找到了答案,并偶然发现了“所有元素 [persisters] ... 按顺序受到质疑”这一行,因此我重新排序了 CreateCacheManager 方法中的条目,现在它正在工作。

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

于 2013-11-22T03:02:05.597 回答