简体   繁体   中英

How to download a file stored on Google Drive from an Android app?

I am writing a android app that needs to download a file from the web upon app launch.

This is the method that is used to download the file.

private InputStream downloadUrl(String urlString) throws IOException { URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(10000); conn.setConnectTimeout(15000); conn.setRequestMethod("GET"); conn.setDoInput(true); conn.connect(); return conn.getInputStream(); }

I have tried the following without much success.

  • Share the file on Google drive (Public on the web), and use the given "share URL" to download - Gives an error - Connection refused

  • Publish the file on web (via Google Drive) and use the given "publish URL" to download - Got this error - Unable to resolve host docs.google.com

Is there a possibility to use Google drive for this purpose?

Thanks in Advance.

You'd like to use DriveApi.getFile and DriveFile.openContents to retrieve the file in Google Drive. As far as I know, you cannot download the file directly via url.

I'm using the code like this:

    DriveFile file = Drive.DriveApi.getFile(client, driveId);
    DriveApi.ContentsResult contentsResult = file.openContents(client, DriveFile.MODE_READ_ONLY, null).await();
    if (!contentsResult.getStatus().isSuccess()) {
        error = new Error("failed in retrieving the file content for " + driveId);
        return null;
    }

    return getInputStream();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM