[英]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. 我正在编写一个Android应用程序,该应用程序启动后需要从网上下载文件。
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 在Google驱动器上共享文件(在网络上公开),然后使用给定的“共享URL”进行下载-出现错误-连接被拒绝
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 将文件发布到网络上(通过Google云端硬盘),并使用给定的“发布URL”进行下载-出现此错误-无法解析主机docs.google.com
Is there a possibility to use Google drive for this purpose? 是否可以为此目的使用Google驱动器?
Thanks in Advance. 提前致谢。
You'd like to use DriveApi.getFile and DriveFile.openContents to retrieve the file in Google Drive. 您想使用DriveApi.getFile和DriveFile.openContents在Google云端硬盘中检索文件。 As far as I know, you cannot download the file directly via url. 据我所知,您不能直接通过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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.