Google drive Android Api requested dimension image download

房东的猫 提交于 2019-12-24 10:38:40

问题


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:


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.



来源:https://stackoverflow.com/questions/28472349/google-drive-android-api-requested-dimension-image-download

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!