当時の Picasso のバージョンは、私が望んでいたものをサポートしていなかったので、Glide 3を使用することにしました。なんとか組み上げたのがこちら。
私はまだテストしていませんでした。履歴からまとめました。アプリからこのサポートを削除したためです。やり過ぎでした。ただし、機能がまだ存在していたときに機能していました。
でConnectionCallbacks.onConnected
:
Glide.get(getContext()).register(DriveId.class, InputStream.class, new DriveIdModelLoader.Factory(mClient));
でConnectionCallbacks.onConnectionSuspended
:
Glide.get(getContext()).unregister(DriveId.class, InputStream.class);
リスト内Adapter
:
String driveString = cursor.getString(cursor.getColumnIndex("image"));
DriveId driveId = DriveId.decodeFromString(driveString);
Glide.with(this)
.from(DriveId.class)
.load(driveId)
.into(imageView);
グライドグルー:
import java.io.*;
import android.content.Context;
import android.net.Uri;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.*;
import com.bumptech.glide.load.model.stream.StreamModelLoader;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.drive.DriveId;
public class DriveIdModelLoader implements StreamModelLoader<DriveId> {
private final GoogleApiClient client;
public DriveIdModelLoader(GoogleApiClient client) {
this.client = client;
}
public DataFetcher<InputStream> getResourceFetcher(DriveId model, int width, int height) {
return new DriveIdDataFetcher(client, model);
}
public static class Factory implements ModelLoaderFactory<DriveId, InputStream> {
private final GoogleApiClient client;
public Factory(GoogleApiClient client) {
this.client = client;
}
public ModelLoader<DriveId, InputStream> build(Context context, GenericLoaderFactory factories) {
return new DriveIdModelLoader(client);
}
public void teardown() {
client.disconnect();
}
}
}
ドライブ関連のグライド ロジック:
import java.io.InputStream;
import org.slf4j.*;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.data.DataFetcher;
import com.google.android.gms.common.api.*;
import com.google.android.gms.drive.*;
public class DriveIdDataFetcher implements DataFetcher<InputStream> {
private static final Logger LOG = LoggerFactory.getLogger(DriveIdDataFetcher.class);
private final GoogleApiClient client;
private final DriveId driveId;
private boolean cancelled = false;
private DriveFile file;
private Contents contents;
public DriveIdDataFetcher(GoogleApiClient client, DriveId driveId) {
this.client = client;
this.driveId = driveId;
}
public String getId() {
return driveId.encodeToString();
}
public InputStream loadData(Priority priority) {
if (cancelled) return null;
if (client == null) {
LOG.warn("No connected client received, giving custom error image");
return null;
}
file = Drive.DriveApi.getFile(client, driveId);
if (cancelled) return null;
contents = sync(file.openContents(client, DriveFile.MODE_READ_ONLY, null)).getContents();
if (cancelled) return null;
return contents.getInputStream();
}
public void cancel() {
cancelled = true;
if (contents != null) {
file.discardContents(client, contents);
}
}
public void cleanup() {
if (contents != null) {
sync(file.discardContents(client, contents));
}
}
private static <T extends Result> void assertSuccess(T result) {
if (!result.getStatus().isSuccess()) {
throw new IllegalStateException(result.getStatus().toString());
}
}
private static <T extends Result> T sync(PendingResult<T> pending) {
T result = pending.await();
assertSuccess(result);
return result;
}
}