Google drive Android Api requested dimension image

2019-08-02 02:27发布

I'm trying to create a Google drive android app where I should be able to download resized images from Google drive.

It's kind of a performance issue. Suppose I have uploaded an image of 1080px from my computer, now I wish to download that image to my Android phone which doesn't support 1080px image, so what happens is the image first gets downloaded then it's resized as per my device dimensions. But this is actually wastage of time and data.

Why to download a 1080px image when I can only show may be like 480px or 720px image.

I'm using Google drive api but I would like to know whether there is some parameter or something which we can pass from phone so that we get to download the resized image only.

As per my search, Link

There is this Partial download where I can give a range of bytes but I'm not trying to download a particular portion of a file, I want to download the whole image file in a resized format.

Any ideas, clues will be very helpful.

Thanks

1条回答
乱世女痞
2楼-- · 2019-08-02 02:47

The answer applies to the RESTful Api, since GDAA does not have a 'thumbnailLink' functionality as of today (Feb 14. 2015).

I use this code snippet to download reduced images and thumbnails:

/**
 * get file contents, specifically image. The thmbSz, if not zero, it attempts to 
 * retrieve an image thumbnail that fits into a square envelope of thmbSz pixels
 * @param rsId   file id
 * @param thmbSz reduced size envelope (optional value, 0 means full size image / file)
 *   (tested with: 128,220,320,400,512,640,720,800,1024,1280,1440,1600)
 * @return       file's content  / null on fail
 */
com.google.api.services.drive.Drive mGOOSvc;
...
static byte[] read(String rsId, int thmbSz) {
  byte[] buf = null;
  if (rsId != null) try {
    File gFl = (thmbSz == 0) ?
      mGOOSvc.files().get(rsId).setFields("downloadUrl").execute() :
      mGOOSvc.files().get(rsId).setFields("thumbnailLink").execute();
    if (gFl != null){
      String strUrl;
      if (thmbSz == 0)
         strUrl = gFl.getDownloadUrl();
      else {
         strUrl = gFl.getThumbnailLink();
         if (! strUrl.endsWith("s220")) return null; //--- OOPS ------------>>>
         strUrl = strUrl.substring(0, strUrl.length()-3) + Integer.toString(thmbSz);
      }
      InputStream is = mGOOSvc.getRequestFactory()
                 .buildGetRequest(new GenericUrl(strUrl)).execute().getContent();
      buf = UT.is2Bytes(is);
    }
  } catch (Exception e) { UT.le(e); }
  return buf;
}

I have a tested it with a few sizes (see the method header), which I snatched from Picasa documentation here.

See, the 'thumbnailLink' has the form of

https://lh4.googleusercontent.com/R1Pi...blahblah...08qIhg=s220

and by changing the '220' at the end to one of these values:

128,220,320,400,512,640,720,800,1024,1280,1440,1600

I managed to pull the image of the requested size (letting the server to reduce it).

DISCLAIMER:

It is not a documented feature, so it is a HACK. I also admit that a more robust version should not rely on finding "s220", but probably "=sNUMBER" patttern. Good luck.

查看更多
登录 后发表回答