It's not great, but I'll put it up here in case anyone else runs into this kind of problem on CBAndroid and Picasso. For any other users, they would need to inject any more information about their setup (e.g. in my actual code, I retro-fitted the Uri to actually contain docId and attachment name, but I'm ignoring that code for now).
public class CouchbaseDownloader implements Downloader {
private Database database;
public CouchbaseDownloader(Database database) {
this.database = database;
}
@Override
public Response load(Uri uri, boolean localCacheOnly) throws IOException {
try {
Attachment image = database.getDocument(uri.toString()).getCurrentRevision().getAttachment("image");
InputStream stream = image.getContent();
return new Response(stream, true, image.getLength());
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
return null;
}
}
I also created a singleton wrapper for Picasso - this is strictly due to how this app is currently structured, and where the Database becomes available. Ideally, using Dagger, you could inject this in a base module, but I'm stuck with this wrapper solution until I can get around to a re-factoring...
public class CBPicasso {
private static Picasso singleton = null;
// Using the same fluent API call as Picasso proper
public static Picasso with(Context context, Database database) {
if (singleton == null) {
synchronized (CBPicasso.class) {
if (singleton == null) {
singleton = new Picasso
.Builder(context)
.downloader(new CouchbaseDownloader(database))
.build();
}
}
}
return singleton;
}
}