使用插入功能将文件上传到Google驱动器问题

时间:2017-03-15 12:54:32

标签: java google-drive-api

我正在尝试将文件上传到Google云端硬盘

我从这里得到的部分代码 GoogleDriveUpload

这是有错误的代码的一部分。

private File insertFile(com.google.api.services.drive.Drive service, String title, String description,
                        String parentId, String mimeType, final String filename) {
    // File's metadata.
    File body = new File();
    body.setTitle(title);
    body.setDescription(description);
    body.setMimeType(mimeType);

    // Set the parent folder.
    if (parentId != null && parentId.length() > 0) {
        body.setParents(Collections.singletonList(new ParentReference().setId(parentId)));
    }

    // File's content.
    java.io.File fileContent = new java.io.File(filename);
    if(!fileContent.canRead()) {
        ((Activity)context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Dialog wrong = new Dialog(context);
                wrong.setTitle("file ("+filename+") is not readable");
                wrong.show();
            }
        });
        return null;
    }


    FileContent mediaContent = new FileContent(mimeType, fileContent);

    try {
        com.google.api.services.drive.Drive.Files.Insert insert = service.files().insert(body,mediaContent);

        MediaHttpUploader uploader = insert.getMediaHttpUploader();
        uploader.setChunkSize(1 * 1024 * 1024);
        uploader.setProgressListener(new FileUploadProgressListener(filename));

        File file = insert.execute();

        return file;
    } catch (IOException e) {
        Log.e(TAG, "ERROR OCCURED : " + e);
        return null;
    }
}

我无法构建项目并出现错误:

Error:(464, 13) error: cannot find symbol method setTitle(String)
Error:(470, 54) error: incompatible types: List<ParentReference> cannot be converted to List<String>
Error:(491, 54) error: cannot find symbol class Insert
Error:(491, 86) error: cannot find symbol method insert(File,FileContent)

有什么想法吗?

0 个答案:

没有答案