get Google Drive Folder's ResourceID

◇◆丶佛笑我妖孽 提交于 2019-12-23 05:40:26

问题


I need to get the resourceId of a folder created inside my drive. Referred these docs first and second I am doing this :

 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

                                        });
                            }
                        }
                    }
                });

With this code, I am getting every value inside result except for the getResourceId()( which is null always when I get it through code).

Well I can even see my folder's id in drive url.

Already done a lot of research but none of them give any solution yet.They are giving only REASONS.

Is there anyone who have solved this ?


回答1:


As the answer of @seanpj at Google Drive Android Api Completion Event for Folder Creation, you can try as below although perhaps it's not a very nice solution for now.

Inside <application> of AndroidManifest.xml:

<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());
    }
}

Inside 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 info:

File ResourceId: 0B0sy1Vw2EUQINkdJRUR2R0puVDg
Folder ResourceId: 0B0sy1Vw2EUQIV3F2Zzl6RUdBdms


来源:https://stackoverflow.com/questions/38012719/get-google-drive-folders-resourceid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!