[英]Uploading file to specific folder in Google Drive
我有兩種方法,一種用於創建文件並將其上傳到根文件夾到 Google 驅動器,第二種方法只是創建一個文件夾到 Google 驅動器。
是否有可能以某種方式連接這兩者並將文件上傳到新創建的文件夾?
我已經閱讀了谷歌文檔,但我不知道在哪里可以更改該文件所在的文件夾
創建文件:
private Task<Void> createFileIntentSender(DriveContents driveContents, File file) throws FileNotFoundException {
Log.i(TAG, "New contents created.");
// Get an output stream for the contents.
OutputStream outputStream = driveContents.getOutputStream();
byte[] bytesArray = new byte[(int) file.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
fis.read(bytesArray); //read file into bytes[]
fis.close();
outputStream.write(bytesArray);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle(windowName.getText().toString())
.setMimeType("application/vnd.ms-excel")
.setStarred(true)
.build();
// Set up options to configure and display the create file activity.
CreateFileActivityOptions createFileActivityOptions =
new CreateFileActivityOptions.Builder()
.setInitialMetadata(changeSet)
.setInitialDriveContents(driveContents)
.build();
return mDriveClient
.newCreateFileActivityIntentSender(createFileActivityOptions)
.continueWith(
new Continuation<IntentSender, Void>() {
@Override
public Void then(@NonNull Task<IntentSender> task) throws Exception {
startIntentSenderForResult(task.getResult(), REQUEST_CODE_CREATOR, null, 0, 0, 0);
return null;
}
});
}
創建文件夾:
private void createFolder() {
mDriveResourceClient.getRootFolder()
.continueWithTask(task -> {
DriveFolder parentFolder = task.getResult();
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("Folder1")
.setMimeType(DriveFolder.MIME_TYPE)
.setStarred(true)
.build();
return mDriveResourceClient.createFolder(parentFolder, changeSet);
})
.addOnFailureListener(this, e -> {
Log.e(TAG, "Unable to create file", e);
finish();
});
}
無需先將文件上傳到root
文件夾。 您可以將文件直接上傳到所需的文件夾。
首先,您需要創建要將文件上傳到的文件夾。 此方法返回一個文件資源( files
和folders
在 Drive 中基本相同,主要區別在於folders
僅包含元數據並且具有 MIME 類型application/vnd.google-apps.folder
)。
其次,您必須將文件直接上傳到創建的文件夾。 為此,在進行調用時,您必須在文件的parents
屬性中指定folder ID
(您可以從上一步的響應中獲取)(選中此項以查看您可以在文件中指定的所有屬性) .
我希望這對你有任何幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.