Skip to content Skip to sidebar Skip to footer

How To Download Text File From Google Drive Using The Url By The Google Drive Api For Android

I have uploaded a text file to my google drive account and it is public. I got the link for that file by the share link. What I want to do is to download that file from the drive u

Solution 1:

There are 2 different APIs to access Google Drive from Android. The GDAA and the REST API. You need he REST since GDAA supports only FILE scope (i.e. can only see files/folders the Android app created).
Assuming you are asking about the 'shareable link' that looks like this:

https://drive.google.com/file/d/0B1mQ................cW90ODA/view?usp=sharing

the URL portion below (most of the original is replaced by dots, to keep my stuff private):

0B1mQ................cW90ODA

is the actual (resource) ID that is used in REST to retrieve files, folders. See this snippet:

 /*************************************************************************
   * get file contents
   * @param resId  file driveId
   * @return       file's content  / null on fail
   */
  static byte[] read(String resId) {
    byte[] buf = null;
    if (mGOOSvc != null && mConnected && resId != null) try {
      File gFl = mGOOSvc.files().get(resId).setFields("downloadUrl").execute();
      if (gFl != null){
        String strUrl = gFl.getDownloadUrl();
        InputStream is = mGOOSvc.getRequestFactory()
        .buildGetRequest(new GenericUrl(strUrl)).execute().getContent();
        buf = UT.is2Bytes(is);
      }
    } catch (Exception e) { UT.le(e); }
    return buf;
  }

taken out of context from the REST class of the GDAA/REST demo here.

You don't need bother with the demo, you can test it in the playground here (bottom of the page).

Good Luck


Post a Comment for "How To Download Text File From Google Drive Using The Url By The Google Drive Api For Android"