如何获得文件ID,所以我可以从谷歌云端硬盘API在Android上执行下载的文件 [英] How to get the file ID so I can perform a download of a file from Google Drive API on Android
问题描述
我工作的一个机器人项目,我想利用谷歌云端硬盘API的,我已经得到了大部分工作,但我有在问题要如何进行下载。
I am working on an android project and I am trying to make use of the Google Drive API and I've got most of it working but I am having an issue in how I perform a download.
我找不到任何地方我是怎么得到的文件ID,这样我可以进行下载。为了测试我检索到的所有文件在我的硬盘帐户,并将其添加到列表数组,然后得到了ID在列表中的第一个文件。
I can't find anywhere how I get the file ID so that I can perform the download. In order to test I've retrieved all files in my drive account and added them to a list array and then got the ID for the first file in the list.
我再复制文件ID到命令下载文件,而这成功的工作,但我不知道如何让特定的文件我想下载的文件ID。
I then copied the file ID in to the command to download the file and this worked successfully but I have no idea how to get the file id of the specific file I want to download.
下面是code我使用下载的文件。
Below is the code I am using to download the file.
private void downloadFile()
{
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try
{
com.google.api.services.drive.model.File file = service.files().get("0B-Iak7O9SfIpYk9zTjZvY2xreVU").execute();
//FileList file = service.files().list().execute();
//List<com.google.api.services.drive.model.File> fileList = file.getItems();
//com.google.api.services.drive.model.File fileItem = fileList.get(0);
//Log.d("FileID" , fileItem.getId());
//Log.d("Count", "Retreived file list");
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0)
{
HttpResponse resp = service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl())).execute();
InputStream inputStream = resp.getContent();
writeToFile(inputStream);
}
}
catch (IOException e)
{
Log.e("WriteToFile", e.toString());
e.printStackTrace();
}
}
});
thread.start();
}
0B-Iak7O9SfIpYk9zTjZvY2xreVU
是我下载的文件时,我做了一个列表,选择第一个文件,我检索到的文件ID,但我怎么能说我想下载,File_1并获得其ID来那么这传递给service.get()函数执行。
0B-Iak7O9SfIpYk9zTjZvY2xreVU
is the file ID of the file that I download which I retrieved when I did a list and selected the first file, but how can I say I want to download, File_1 and get its ID to then pass this to the service.get().execute function.
基本上我的最终目标是我的应用程序中,我上传一个XML文件来谷歌驱动器,然后后来就下载文件。这将只有一个文件,将始终具有相同的名称。我要去一下吧马上或是否有更好的方法来实现我想做的事情。
Basically my end goal is within my app, I upload an XML file to Google Drive, and then later on Download the file. It will only be one file and will always have the same name. Am I going about it the right away or is there a better way to achieve what I am trying to do.
我希望这是有道理的,任何帮助将大大AP preciated。
I hope this makes sense, any help would be greatly appreciated.
感谢
推荐答案
嗯,我能想到的第一个选项是,你可以使用的列表要求=https://developers.google.com/drive/search-parameters相对=nofollow>搜索参数的为您的文件,如标题=File_1.xml和fileExtension =XML
。它要么返回文件的空列表(没有一个匹配检索算法的标准),或具有至少一个文件返回的列表。如果是只有一个 - 它很容易。但是,如果有更多的 - 你必须选择基于一些其他领域的其中之一。请记住,在你的GDrive可能有超过1个文件具有相同的名称。所以你提供更多的搜索参数,就更好了。
Well the first option I could think of is that you could send a list
request with search parameters for your file, like title="File_1.xml" and fileExtension="xml"
. It will either return an empty list of files (there isn't one matching the serach criteria), or return a list with at least one file. If it's only one - it's easy. But if there are more - you'll have to select one of them based on some other fields. Remember that in gdrive you could have more than 1 file with the same name. So the more search parameters you provide, the better.
这篇关于如何获得文件ID,所以我可以从谷歌云端硬盘API在Android上执行下载的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!