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

前端之家收集整理的这篇文章主要介绍了如何在Android上将Google云端硬盘与Picasso集成?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在编写一个应用程序,将图像存储在Google云端硬盘中,我想显示这些图库(GridView).为了更高效(即异步),我想将其与毕加索结合起来.但是Picasso.load(String)只是一个加载的别名(Uri.parse(path)),我没有Uri,因为图像是这样加载的(简化和未公开的实用程序方法):
  1. public static Bitmap getBitmap(DriveId id) {
  2. GoogleApiClient client = ApiClientAsyncTask.createConnectedClient(App.getAppContext());
  3. DriveFile file = Drive.DriveApi.getFile(client,id);
  4. Metadata Meta = sync(file.getMetadata(client));
  5. Contents contents = sync(file.openContents(client,DriveFile.MODE_READ_ONLY,null));
  6. InputStream imageStream = contents.getInputStream();
  7. try {
  8. return BitmapFactory.decodeStream(imageStream);
  9. } finally {
  10. IOTools.ignorantClose(imageStream);
  11. sync(file.discardContents(client,contents));
  12. }
  13. }

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

当然我想在Picasso中使用缓存(网络(上面的方法),磁盘,内存)的完全支持.

解决方法

当时毕加索的版本不支持我想要的东西所以我去使用了 Glide 3.这就是我设法放在一起的东西.
我还没有测试它,我把它从历史中拼凑起来,因为我从我的应用程序中删除了这个支持,这是一个过度杀伤力;但是当功能仍然存在时,它正在工作.

在ConnectionCallbacks.onConnected中:

  1. Glide.get(getContext()).register(DriveId.class,InputStream.class,new DriveIdModelLoader.Factory(mClient));

在ConnectionCallbacks.onConnectionSuspended中:

  1. Glide.get(getContext()).unregister(DriveId.class,InputStream.class);

在列表适配器:

  1. String driveString = cursor.getString(cursor.getColumnIndex("image"));
  2. DriveId driveId = DriveId.decodeFromString(driveString);
  3. Glide.with(this)
  4. .from(DriveId.class)
  5. .load(driveId)
  6. .into(imageView);

滑胶:

  1. import java.io.*;
  2.  
  3. import android.content.Context;
  4. import android.net.Uri;
  5.  
  6. import com.bumptech.glide.load.data.DataFetcher;
  7. import com.bumptech.glide.load.model.*;
  8. import com.bumptech.glide.load.model.stream.StreamModelLoader;
  9. import com.google.android.gms.common.api.GoogleApiClient;
  10. import com.google.android.gms.drive.DriveId;
  11.  
  12. public class DriveIdModelLoader implements StreamModelLoader<DriveId> {
  13. private final GoogleApiClient client;
  14.  
  15. public DriveIdModelLoader(GoogleApiClient client) {
  16. this.client = client;
  17. }
  18.  
  19. public DataFetcher<InputStream> getResourceFetcher(DriveId model,int width,int height) {
  20. return new DriveIdDataFetcher(client,model);
  21. }
  22.  
  23. public static class Factory implements ModelLoaderFactory<DriveId,InputStream> {
  24. private final GoogleApiClient client;
  25.  
  26. public Factory(GoogleApiClient client) {
  27. this.client = client;
  28. }
  29.  
  30. public ModelLoader<DriveId,InputStream> build(Context context,GenericLoaderFactory factories) {
  31. return new DriveIdModelLoader(client);
  32. }
  33.  
  34. public void teardown() {
  35. client.disconnect();
  36. }
  37. }
  38. }

驱动相关的Glide逻辑:

  1. import java.io.InputStream;
  2.  
  3. import org.slf4j.*;
  4.  
  5. import com.bumptech.glide.Priority;
  6. import com.bumptech.glide.load.data.DataFetcher;
  7. import com.google.android.gms.common.api.*;
  8. import com.google.android.gms.drive.*;
  9.  
  10. public class DriveIdDataFetcher implements DataFetcher<InputStream> {
  11. private static final Logger LOG = LoggerFactory.getLogger(DriveIdDataFetcher.class);
  12.  
  13. private final GoogleApiClient client;
  14. private final DriveId driveId;
  15.  
  16. private boolean cancelled = false;
  17.  
  18. private DriveFile file;
  19. private Contents contents;
  20.  
  21. public DriveIdDataFetcher(GoogleApiClient client,DriveId driveId) {
  22. this.client = client;
  23. this.driveId = driveId;
  24. }
  25.  
  26. public String getId() {
  27. return driveId.encodeToString();
  28. }
  29.  
  30. public InputStream loadData(Priority priority) {
  31. if (cancelled) return null;
  32. if (client == null) {
  33. LOG.warn("No connected client received,giving custom error image");
  34. return null;
  35. }
  36. file = Drive.DriveApi.getFile(client,driveId);
  37. if (cancelled) return null;
  38. contents = sync(file.openContents(client,null)).getContents();
  39. if (cancelled) return null;
  40. return contents.getInputStream();
  41. }
  42.  
  43. public void cancel() {
  44. cancelled = true;
  45. if (contents != null) {
  46. file.discardContents(client,contents);
  47. }
  48. }
  49.  
  50. public void cleanup() {
  51. if (contents != null) {
  52. sync(file.discardContents(client,contents));
  53. }
  54. }
  55.  
  56. private static <T extends Result> void assertSuccess(T result) {
  57. if (!result.getStatus().isSuccess()) {
  58. throw new IllegalStateException(result.getStatus().toString());
  59. }
  60. }
  61.  
  62. private static <T extends Result> T sync(PendingResult<T> pending) {
  63. T result = pending.await();
  64. assertSuccess(result);
  65. return result;
  66. }
  67. }

猜你在找的Android相关文章