Android Picasso Dropbox:如何将图像加载到gridview中

时间:2015-09-11 11:30:13

标签: android dropbox picasso

  1. 我正在使用Dropbox Android SDK获取照片并将其显示到GridView中。
  2. 目前,我正在使用毕加索作为适配器和其他图片资源,如GPhotos,Facebook ......也是。
  3. 如何从Dropbox的文件中获取可下载的链接?或者如何使用Picasso加载Dropbox的图像?
    这是我获取Dropbox图像的方式:

    List<Entry> listEntry = mApi.search("/photos", ".jpg", 0, false);
    
    int i = 0;
    for (Entry entry : listEntry) {
        if (this.isRemoving()) { // Check if fragment is being removed
            return;
        }
        if (!entry.isDir) { // Check if this entry is dir or file
            i++;
            Log.e(TAG, entry + " --- " + entry.fileName() + " --- "
                    + entry.parentPath() + " --- " + entry.modified);
            final DropboxLink shareLink = mApi.share(entry.parentPath()
                    + entry.fileName());
            url = shareLink.url;
            Log.v(TAG, "shareLink: " + shareLink.url);
            // The file name I get is ImageName.JPG and path is /Photos
            // With share() the url I get looks like https://db.tt/xxxxxxxx
        }
    }
    

3 个答案:

答案 0 :(得分:0)

我无法提供有关使用Picasso的见解,但如果您可以直接接受文件数据,则使用getFilegetThumbnail代替share会更好。

如果您确实需要Picasso可以直接下载的网址,则应使用media方法。

答案 1 :(得分:0)

Thanks @Greg for suggestion, i have a little notice for using Picasso with Dropbox SDK.

When use media i have to use option TRUE in SSL for success loading image from Picasso, if FALSE Picasso cannot load links.

答案 2 :(得分:0)

Dropbox中有一个示例,请尝试检查此链接dropbox sample

您需要看到2个文件 PicassoClient .java和 FileThumbnailRequestHandler .java

这里是代码:

PicassoClient类:

public class PicassoClient {
    private static Picasso sPicasso;

    public static void init(Context context, DbxClientV2 dbxClient) {

        // Configure picasso to know about special thumbnail requests
        sPicasso = new Picasso.Builder(context)
                .downloader(new OkHttpDownloader(context))
                .addRequestHandler(new FileThumbnailRequestHandler(dbxClient))
                .build();
    }


    public static Picasso getPicasso() {
        return sPicasso;
    }
}

FileThumbnailRequestHandler类:

public class FileThumbnailRequestHandler extends RequestHandler {

    private static final String SCHEME =  "dropbox";
    private static final String HOST = "dropbox";
    private final DbxClientV2 mDbxClient;

    public FileThumbnailRequestHandler(DbxClientV2 dbxClient) {
        mDbxClient = dbxClient;
    }

    /**
     * Builds a {@link Uri} for a Dropbox file thumbnail suitable for handling by this handler
     */
    public static Uri buildPicassoUri(FileMetadata file) {
        return new Uri.Builder()
                .scheme(SCHEME)
                .authority(HOST)
                .path(file.getPathLower()).build();
    }

    @Override
    public boolean canHandleRequest(Request data) {
        return SCHEME.equals(data.uri.getScheme()) && HOST.equals(data.uri.getHost());
    }

    @Override
    public Result load(Request request, int networkPolicy) throws IOException {

        try {
            DbxDownloader<FileMetadata> downloader =
                    mDbxClient.files().getThumbnailBuilder(request.uri.getPath())
                            .withFormat(ThumbnailFormat.JPEG)
                            .withSize(ThumbnailSize.W1024H768)
                            .start();

            return new Result(downloader.getInputStream(), Picasso.LoadedFrom.NETWORK);
        } catch (DbxException e) {
            throw new IOException(e);
        }
    }
}

您只需要导入上面提到的那两个类然后获取sPicasso对象,然后您就可以使用它了。 :)