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

编程入门 行业动态 更新时间:2024-10-26 20:34:29
本文介绍了如何在Android上将Google云端硬盘与Picasso集成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在编写一个将图像存储在Google云端硬盘中的应用,我想显示其中的图像库( GridView )。为了提高性能(即异步),我想将其与 Picasso 集成。但是 Picasso.load(String)只是 load(Uri.parse(path))的别名,并且我没有 Uri ,因为图像是这样加载的(简化并使用未公开的实用程序方法):

I'm writing an app which stores images in Google Drive and I'd like to display a gallery of those (GridView). To be more performant (i.e. async) I'd like to integrate this with Picasso. But the Picasso.load(String) is just an alias to load(Uri.parse(path)), and I don't have a Uri because the images are loaded like this (simplified and with undisclosed utility methods):

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

是否还有其他类似的库可能支持任意库的集成输入(字符串/对象)?

Is there any other similar library maybe supporting integration of arbitrary inputs (String/Object)?

我当然想在毕加索中使用缓存(网络(上述方法),磁盘,内存)的全部支持。

Of course I'd like to use the full support of caching (network (the above method), disk, memory) in Picasso.

推荐答案

当时的毕加索版本不支持我想要的版本,所以我使用了滑行3 。这是我设法整理的。 我还没有测试它,我从历史中整理了一下,因为我从我的应用程序中删除了此支持,这太过分了;

The version of Picasso at the time didn't support what I wanted so I went to use Glide 3. Here's what I managed to put together. I didn't test it yet, I pieced it together from history, because I removed this support from my app, it was an overkill; however it was working when the functionality was still present.

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

与驱动相关的滑行逻辑:

Drive related Glide logic:

import java.io.InputStream; import org.slf4j.*; import com.bumptech.glide.Priority; import com.bumptech.glide.load.data.DataFetcher; import com.google.android.gmsmon.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; } }

更多推荐

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

本文发布于:2023-10-26 11:04:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529947.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:云端   上将   硬盘   如何在   Google

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!