如何在Android上将Google云端硬盘与Picasso集成?

我正在编写一个应用程序,将图像存储在Google云端硬盘中,我想显示这些图库(GridView).为了更高效(即异步),我想将其与毕加索结合起来.但是Picasso.load(String)只是一个加载的别名(Uri.parse(path)),我没有Uri,因为图像是这样加载的(简化和未公开的实用程序方法):
public static Bitmap getBitmap(DriveId id) {
    Googleapiclient client = apiclientAsyncTask.createConnectedClient(App.getAppContext());
    DriveFile file = Drive.DriveApi.getFile(client,id);
    Metadata Meta = sync(file.getMetadata(client));
    Contents contents = sync(file.openContents(client,DriveFile.MODE_READ_ONLY,null));
    InputStream imagestream = contents.getInputStream();
    try {
        return BitmapFactory.decodeStream(imagestream);
    } finally {
        IOTools.ignorantClose(imagestream);
        sync(file.discardContents(client,contents));
    }
}

是否有任何其他类似的库可能支持任意输入(String / Object)的集成?

当然我想在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);

在列表适配器:

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();
        }
    }
}

驱动相关的Glide逻辑:

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,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;
    }
}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...