保存在特定的文件夹中的文件与谷歌驱动器SDK [英] Save file in specific folder with Google Drive SDK
问题描述
我一直想一个纯文本文件保存成谷歌驱动器在Android上的特定文件夹。
I've been trying to save a plain text file into a specific folder in Google Drive on Android.
到目前为止,使用文档和快速入门指南在谷歌驱动器我已经能够这样做会在正确的方向上的几件事情,第一我能创造一个纯文本文件:
So far using the Documentation and QuickStart Guide on Google Drive I have been able to do a few things that are going in the right direction, first I was able to create a plain text file:
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("text/plain");
File file = service.files().insert(body, textContent).execute();
我已经能够在谷歌驱动器与根目录创建一个新的文件夹:
I have been able to create a new folder in the base directory of Google Drive with:
File body = new File();
body.setTitle("Air Note");
body.setMimeType("application/vnd.google-apps.folder");
File file = service.files().insert(body).execute();
我还能够列出所有用户的谷歌驱动器中的文件夹与帐户:
I have also been able to list all of the folders in the user's Google Drive account with:
List<File> files = service.files().list().setQ("mimeType = 'application/vnd.google-apps.folder'").execute().getItems();
for (File f : files) {
System.out.println(f.getTitle() + ", " + f.getMimeType());
}
不过我有点停留在如何将文本文件保存为中谷歌驱动器的文件夹。
However I am a bit stuck on how to save a text file into a folder within Google Drive.
推荐答案
您需要使用parent参数使用插入把一个文件一个文件夹中。更多详情请见<一href="https://developers.google.com/drive/v2/reference/files/insert">https://developers.google.com/drive/v2/reference/files/insert
You need to use the parent parameter to put a file in a folder using insert. More details at https://developers.google.com/drive/v2/reference/files/insert
像这样
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("text/plain");
body.setParents(Arrays.asList(new File.ParentReference().setId(parentId));
File file = service.files().insert(body, textContent).execute();
这篇关于保存在特定的文件夹中的文件与谷歌驱动器SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!