如何获取文件 ID,以便我可以从 Android 上的 Google Drive API 下载文件? [英] How to get the file ID so I can perform a download of a file from Google Drive API on Android?

查看:15
本文介绍了如何获取文件 ID,以便我可以从 Android 上的 Google Drive API 下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Android 项目,我正在尝试使用 Google Drive 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.

下面是我用来下载文件的代码.

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().execute 函数.

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 文件上传到 Google Drive,然后再下载该文件.它只会是一个文件,并且始终具有相同的名称.我是马上去做还是有更好的方法来实现我想要做的事情?

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?

推荐答案

我能想到的第一个选项是你可以发送一个 list 请求与 搜索参数 为您的文件,如 title="File_1.xml" 和 fileExtension="xml".它要么返回一个空的文件列表(没有一个匹配 serach 条件),要么返回一个包含至少一个文件的列表.如果它只有一个 - 这很容易.但如果还有更多 - 您必须根据其他一些字段选择其中一个.请记住,在 gdrive 中,您可以拥有多个同名文件.因此,您提供的搜索参数越多越好.

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,以便我可以从 Android 上的 Google Drive API 下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆