Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Google Drive的文件夹中创建文件夹?_Java_Android_Google Drive Api_Google Drive Android Api - Fatal编程技术网

Java 如何在Google Drive的文件夹中创建文件夹?

Java 如何在Google Drive的文件夹中创建文件夹?,java,android,google-drive-api,google-drive-android-api,Java,Android,Google Drive Api,Google Drive Android Api,我知道如何使用API v2 for Java在Google Drive中创建文件夹: 但是我如何在文件夹中创建一个文件夹,将我的文件放入其中呢 假设我有一个文件myfile,我想上传到Google Drive中的/myfolder1/myfolder2/目录,我应该首先创建myfolder1,然后获取其ID,然后创建myfolder2并将其父级设置为myfolder1,然后将myfile的父级设置为myfolder1吗? 有更好的方法吗 我也在添加代码: private File insert

我知道如何使用API v2 for Java在Google Drive中创建文件夹:

但是我如何在文件夹中创建一个文件夹,将我的文件放入其中呢

假设我有一个文件myfile,我想上传到Google Drive中的/myfolder1/myfolder2/目录,我应该首先创建myfolder1,然后获取其ID,然后创建myfolder2并将其父级设置为myfolder1,然后将myfile的父级设置为myfolder1吗? 有更好的方法吗

我也在添加代码:

private File insertFile(String filePath) {
    // Set file's metadata on the server
    File body = new File();
    // Title of the file to insert, including the extension.
    body.setTitle(getFileName(filePath) + "." + getExtension(filePath));
    body.setMimeType(null);
    body.setFileExtension(getExtension(filePath));

    // folderList is string array of the name of the parent folders for the  file
    String[] folderList = getFoldersList(filePath);

    File folder1 = new File();
    folder1.setMimeType("application/vnd.google-apps.folder");
    folder1.setTitle(folderList[0]);
    try {
        folder1 = this.service.files().insert(folder1).execute();
    }
    catch (IOException e){
        e.printStackTrace();
    }

    File folder2 = new File();
    folder2.setMimeType("application/vnd.google-apps.folder");
    folder2.setTitle(folderList[1]);
    folder2.setParents(Arrays.asList(new ParentReference().setId(folder1.getId())));
    try {
        folder2 = this.service.files().insert(folder2).execute();
    }
    catch (IOException e){
        e.printStackTrace();
    }

    body.setParents(Arrays.asList(new ParentReference().setId(folder2.getId())));


    // Set file's content.
    java.io.File fileContent = new java.io.File(filePath);
    // Set file's type
    FileContent mediaContent = new FileContent(null, fileContent);

    try {
        File file = this.service.files().insert(body, mediaContent).execute();
        return file;
    } catch (IOException e) {
        Log.v(DEBUG_TAG, "An error occurred while uploading.");
        e.printStackTrace();
        return null;
    }
}

谢谢

这里有一个小功能,您可以使用返回的ID作为您正在创建的文件的父ID,并且该文件将在多个文件夹中创建:)

//此函数获取表示文件父文件夹的字符串数组
//并返回最内部父文件夹的ID
私有字符串makeParentFolders(字符串[]父列表){
文件夹=null;
字符串folderId=null;
for(int i=0;i0){
folder.setParents(Arrays.asList(newparentreference().setId(folderId));
}
试一试{
folder=this.service.files().insert(folder.execute();
folderId=folder.getId();
}
捕获(IOE异常){
e、 printStackTrace();
}
}
return folder.getId();
}

如果有人想出了一个更优雅的方法,请在这里发布。

这里有一个小功能可以处理它,您可以使用返回的ID作为您正在创建的文件的父ID,并且该文件将在多个文件夹中创建:)

//此函数获取表示文件父文件夹的字符串数组
//并返回最内部父文件夹的ID
私有字符串makeParentFolders(字符串[]父列表){
文件夹=null;
字符串folderId=null;
for(int i=0;i0){
folder.setParents(Arrays.asList(newparentreference().setId(folderId));
}
试一试{
folder=this.service.files().insert(folder.execute();
folderId=folder.getId();
}
捕获(IOE异常){
e、 printStackTrace();
}
}
return folder.getId();
}
如果有人想出一个更优雅的方式,请在这里张贴

// This function gets an array of Strings representing a file's parent folders
// And returns the most inner parent folder's ID
private String makeParentFolders(String[] parentList) {

    File folder = null;
    String folderId = null;
    for(int i = 0; i < parentList.length; i++) {
        folder = new File();
        folder.setMimeType(getMimeType("folder"));
        folder.setTitle(parentList[i]);
        if (i > 0) {
            folder.setParents(Arrays.asList(new ParentReference().setId(folderId)));
        }
        try {
            folder = this.service.files().insert(folder).execute();
            folderId = folder.getId();
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }

    return folder.getId();
}