我需要获取在我的驱动器中创建的文件夹的resourceId。 推荐使用这些文档first和second 我这样做:
private void CreateFolderOnGoogleDrive() {
Query query = new Query.Builder()
.addFilter(Filters.and(Filters.eq(
SearchableField.TITLE, "Folder"),
Filters.eq(SearchableField.TRASHED, false)))
.build();
Drive.DriveApi.query(mGoogleApiClient, query)
.setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() {
@Override
public void onResult(DriveApi.MetadataBufferResult result) {
if (!result.getStatus().isSuccess()) {
System.out.println("Cannot create folder in the root.");
} else {
boolean isFound = false;
for (Metadata m : result.getMetadataBuffer()) {
if (m.getTitle().equals("Folder")) {
Toast.makeText(getApplicationContext(), "folder exist", Toast.LENGTH_LONG).show();
isFound = true;
break;
}
}
if (!isFound) {
System.out.println("Folder not found; creating it.");
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("Folder")
.build();
Drive.DriveApi.getRootFolder(mGoogleApiClient)
.createFolder(mGoogleApiClient, changeSet)
.setResultCallback(new ResultCallback<DriveFolder.DriveFolderResult>() {
@Override
public void onResult(DriveFolder.DriveFolderResult result) {
if (!result.getStatus().isSuccess()) {
System.out.println("Error while trying to create the folder");
} else {
System.out.println("Created a folder");
}
}
System.out.println(result.getDriveFolder().getDriveId().getResourceId());
// ALWAYS return as null
});
}
}
}
});
使用这段代码,除了getResourceId()之外,我在result
内得到了每个值(当我通过代码时它总是为null)。
我甚至可以在驱动器网址中看到我的文件夹ID。
已经做了很多研究,但没有一个给出任何解决方案。他们只给出了理由。
有没有人解决过这个问题?
答案 0 :(得分:4)
作为Google Drive Android Api Completion Event for Folder Creation的@seanpj的答案,您可以尝试如下,尽管它现在可能不是一个非常好的解决方案。希望它有所帮助!
AndroidManifest.xml的<application>
内:
<service android:name=".MyDriveEventService" android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.drive.events.HANDLE_EVENT"/>
</intent-filter>
</service>
MyDriveEventService.java:
public class MyDriveEventService extends DriveEventService {
...
@Override
public void onCompletion(CompletionEvent completionEvent) {
DriveId driveId = completionEvent.getDriveId();
Log.d(TAG, "File ResourceId: " + driveId.getResourceId());
DriveId folderDriveId = MainActivity.getParentID(driveId);
if (folderDriveId != null && folderDriveId.getResourceId() != null)
Log.d(TAG, "Folder ResourceId: " + folderDriveId.getResourceId());
}
}
在MainActivity.java中:
@Override
public void onConnected(@Nullable Bundle bundle) {
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("NewFolder").build();
Drive.DriveApi.getRootFolder(mGoogleApiClient).createFolder(
mGoogleApiClient, changeSet).setResultCallback(folderCreatedCallback);
}
public static DriveId getParentID(DriveId driveId) {
MetadataBuffer mdb = null;
DriveApi.MetadataBufferResult mbRslt = driveId.asDriveResource().listParents(mGoogleApiClient).await();
if (mbRslt.getStatus().isSuccess()) try {
mdb = mbRslt.getMetadataBuffer();
if (mdb.getCount() > 0)
return mdb.get(0).getDriveId();
} catch (Exception e) { e.printStackTrace();}
finally {
if (mdb != null) mdb.close();
}
return null;
}
private final ResultCallback<DriveFolder.DriveFolderResult> folderCreatedCallback = new
ResultCallback<DriveFolder.DriveFolderResult>() {
@Override
public void onResult(DriveFolder.DriveFolderResult result) {
if (!result.getStatus().isSuccess()) {
Log.e(TAG, "Error while trying to create the folder");
return;
}
Log.i(TAG, "Created a folder: " + result.getDriveFolder().getDriveId());
Log.i(TAG, "Folder Resource Id: " + result.getDriveFolder().getDriveId().getResourceId()); // null
// Creating an empty file
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("New file")
.setMimeType("text/plain").build();
result.getDriveFolder().createFile(mGoogleApiClient, changeSet, null /* DriveContents */, new ExecutionOptions.Builder()
.setNotifyOnCompletion(true)
.build())
.setResultCallback(new ResultCallback<DriveFolder.DriveFileResult>() {
@Override
public void onResult(@NonNull DriveFolder.DriveFileResult driveFileResult) {
}
});
}
};
Logcat信息:
File ResourceId: 0B0sy1Vw2EUQINkdJRUR2R0puVDg
Folder ResourceId: 0B0sy1Vw2EUQIV3F2Zzl6RUdBdms