如何获取文件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?

查看:85
本文介绍了如何获取文件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,当我做了一个列表并选择了第一个文件时便检索了该文件的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云端硬盘,然后再下载文件.它只会是一个文件,并且始终具有相同的名称.我是马上就去做还是有更好的方法来实现我想要做的事情?

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?

推荐答案

我想到的第一个选择是,您可以使用.它将返回一个空文件列表(没有一个符合搜索标准的文件),或者返回一个包含至少一个文件的列表.如果只有一个-这很容易.但是,如果还有更多-您必须根据其他一些字段选择其中之一.请记住,在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天全站免登陆