简体   繁体   English

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

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. 我正在开发一个Android项目,我正在尝试使用Google Drive API并且我已经完成了大部分工作,但我在如何执行下载时遇到了问题。

I can't find anywhere how I get the file ID so that I can perform the download. 我无法在任何地方找到我如何获取文件ID以便我可以执行下载。 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。

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. 然后我将文件ID复制到命令下载文件,这成功地工作但我不知道如何获取我想下载的特定文件的文件ID。

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 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. 0B-Iak7O9SfIpYk9zTjZvY2xreVU是我下载的文件的文件ID,当我输入列表并选择第一个文件时,我检索到该文件,但是我怎么能说我想下载,File_1并获取其ID然后将其传递给服务。 get()。执行函数。

Basically my end goal is within my app, I upload an XML file to Google Drive, and then later on Download the file. 基本上我的最终目标是在我的应用程序中,我将XML文件上传到Google云端硬盘,然后再下载文件。 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? 我是马上去做的还是有更好的方法来实现我想做的事情?

In my opinion the easiest and fastest way to get a Google Drive file ID is from Google Drive on the web. 在我看来,获取Google云端硬盘文件ID的最简单快捷的方法是通过网络上的Google云端硬盘获取。 Right-click the file name and select Get shareable link . 右键单击文件名,然后选择“ 获取可共享链接” The last part of the link is the file ID. 链接的最后一部分是文件ID。 Then you can cancel the sharing. 然后你可以取消共享。

This script logs all the file names and ids in the drive: 此脚本记录驱动器中的所有文件名和ID:

// Log the name and id of every file in the user's Drive
function listFiles() {
  var files = DriveApp.getFiles();
  while ( files.hasNext() ) {
    var file = files.next();
    Logger.log( file.getName() + ' ' + file.getId() );
  }
}  

Also, the " Files: list " page has a form at the end that lists the metadata of all the files in the drive, that can be used in case you need but a few ids. 此外,“ 文件:列表 ”页面末尾有一个表单,列出了驱动器中所有文件的元数据,可以在需要但只有几个ID时使用。

One way is to associating unique properties with your file while creation. 一种方法是在创建时将唯一属性与文件相关联。

properties = "{ \
    key='somekey' and \
    value='somevalue'
}"

then create a query. 然后创建一个查询。

query = "title = " + "\'" + title + "\'" + \
        AND + "mimeType = " + "\'" + mimeType + "\'" + \
        AND + "trashed = false" + \
        AND + "properties has " + properties

All the file properties(title, etc) already known to you can go here + properties. 您已知的所有文件属性(标题等)都可以在此处+属性。

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" . 我能想到的第一个选项是你可以发送一个带有你文件搜索参数list请求,比如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. 它将返回一个空的文件列表(没有一个匹配serach标准),或返回一个至少包含一个文件的列表。 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. 请记住,在gdrive中,您可以拥有多个具有相同名称的文件。 So the more search parameters you provide, the better. 因此,您提供的搜索参数越多越好。

Click with the right mouse button on the file in your Google Drive. 点击Google云端硬盘中文件的鼠标右键。 Choose the option to get a link which can be shared from the menu. 选择选项以获取可从菜单共享的链接。 You will see the file id now. 您现在将看到文件ID。 Don't forget to undo the share. 不要忘记撤消共享。

Stan0 intial idea is not a good idea. Stan0最初的想法并不是一个好主意。 There can be multiple files with the same name. 可以有多个具有相同名称的文件。 Very error prone implementation. 非常容易出错。 Stan0's second idea is the correct way. Stan0的第二个想法是正确的方法。

When you first upload the file to google drive store its id (in SharedPreferences is probably easiest) for later use 当您首次将文件上传到谷歌驱动器存储时,其ID(在SharedPreferences中可能是最简单的)供以后使用

ie. 即。

file= mDrive.files().insert(body).execute();      //initial insert of file to google drive

whereverYouWantToStoreIt= file.getId(); //now you have the guaranteed unique id 
                                        //of the file just inserted. Store it and use it 
                                        //whenever you need to fetch this file

If you know the the name of the file and if you always want to download that specific file, then you can easily get the ID and other attributes for your desired file from: https://developers.google.com/drive/v2/reference/files/list (towards the bottom you will find a way to run queries). 如果您知道该文件的名称,并且始终要下载该特定文件,则可以从以下网址轻松获取所需文件的ID和其他属性: https//developers.google.com/drive/v2/ reference / files / list (在底部你会找到一种运行查询的方法)。 In the q field enter title = 'your_file_name' and run it. 在q字段中输入title ='your_file_name'并运行它。 You should see some result show up right below and within it should be an "id" field. 你应该看到一些结果显示在下面,其中应该是一个“id”字段。 That is the id you are looking for. 这是你正在寻找的id。

You can also play around with additional parameters from: https://developers.google.com/drive/search-parameters 您还可以使用以下附加参数: https//developers.google.com/drive/search-parameters

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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