創建一個文件夾,然後創建一個文件夾作爲它的父文件夾。
private static GoogleApiClient mGAC;
//...
/*********************************************************************
* create file/folder in GOODrive
* @param parentFolder parent folder, null for root
* @param titl folder name
* @return folder object
*/
static DriveFolder createFolder(DriveFolder parentFolder, String titl) {
DriveId dId = null;
if (mGAC != null && mGAC.isConnected() && titl != null) try {
if (parentFolder == null)
parentFolder = Drive.DriveApi.getRootFolder(mGAC);
if (parentFolder == null) return null; //----------------->>>
MetadataChangeSet meta;
meta = new Builder().setTitle(titl)
.setMimeType("application/vnd.google-apps.folder").build();
DriveFolderResult r1 = parentFolder.createFolder(mGAC, meta).await();
DriveFolder dFld =
(r1 != null) && r1.getStatus().isSuccess() ? r1.getDriveFolder() : null;
if (dFld != null) {
MetadataResult r2 = dFld.getMetadata(mGAC).await();
if ((r2 != null) && r2.getStatus().isSuccess()) {
dId = r2.getMetadata().getDriveId();
}
}
} catch (Exception e) { e.printStackTrace(); }
return dId == null ? null : dId.asDriveFolder();
}
/********************************************************************
* create file in GOODrive
* @param pFldr parent's ID, null for root
* @param titl file name
* @param mime file mime type
* @param file file (with content) to create
* @return file object
*/
static DriveFile createFile(DriveFolder pFldr, String titl, String mime, File file) {
DriveId dId = null;
if (mGAC != null && mGAC.isConnected() && titl != null && mime != null && file != null) try {
if (pFldr == null)
pFldr = Drive.DriveApi.getRootFolder(mGAC);
if (pFldr == null) return null; //----------------->>>
DriveContents cont = file2Cont(null, file);
MetadataChangeSet meta = new Builder().setTitle(titl).setMimeType(mime).build();
DriveFileResult r1 = pFldr.createFile(mGAC, meta, cont).await();
DriveFile dFil = r1 != null && r1.getStatus().isSuccess() ? r1.getDriveFile() : null;
if (dFil != null) {
MetadataResult r2 = dFil.getMetadata(mGAC).await();
if (r2 != null && r2.getStatus().isSuccess()) {
dId = r2.getMetadata().getDriveId();
}
}
} catch (Exception e) { e.printStackTrace(); }
return dId == null ? null : dId.asDriveFile();
}
private static DriveContents file2Cont(DriveContents cont, File file) {
if (file == null) return null; //--------------------->>>
if (cont == null) {
DriveContentsResult r1 = Drive.DriveApi.newDriveContents(mGAC).await();
cont = r1 != null && r1.getStatus().isSuccess() ? r1.getDriveContents() : null;
}
if (cont != null) try {
OutputStream oos = cont.getOutputStream();
if (oos != null) try {
InputStream is = new FileInputStream(file);
byte[] buf = new byte[4096];
int c;
while ((c = is.read(buf, 0, buf.length)) > 0) {
oos.write(buf, 0, c);
oos.flush();
}
}
finally { oos.close();}
return cont; //++++++++++++++++++++++++++++++>>>
} catch (Exception ignore) {}
return null; //--------------------->>>
}
您需要在非UI線程(的AsyncTask)運行它,或者改變等待()■到setResultCallback()■(從here拍攝)。
好運
如果你知道如何獲得Mime類型有什麼用把它作爲一個變量,並在上面的行使用變量來替代「text/plain的」的問題呢? – db579