Google Drive API - Android - How to obtain a drive file id?

て烟熏妆下的殇ゞ 提交于 2019-12-01 16:19:28
Rivero

You could use the DriveAPI Query method, to retrieve any information about an specific file. you will need to define a query object as the following:

Query query = new Query.Builder()
    .addFilter(Filters.eq(SearchableField.TITLE, "HelloWorld.java"))
    .build();

And set a callback function to iterate on the results:

Drive.DriveApi.query(googleApiClient, query)
        .setResultCallback(new OnChildrenRetrievedCallback() {

    @Override
    public void onChildrenRetrieved(MetadataBufferResult result) {
        // Iterate over the matching Metadata instances in mdResultSet
    }
});

You can find more information on the topic here: https://developers.google.com/drive/android/queries

The solution i found for this problem was creating the file from the app. Using the class ("CreateFileActivity.java") from google drive api demo app.

With this class i save the returning Driveid from the new file in a global DriveId variable.

final private ResultCallback<DriveFolder.DriveFileResult> fileCallback = new
        ResultCallback<DriveFolder.DriveFileResult>() {
            @Override
            public void onResult(DriveFolder.DriveFileResult result) {
                if (!result.getStatus().isSuccess()) {
                    Log.e("","Error while trying to create the file");
                    return;
                }
                Id=result.getDriveFile().getDriveId();
                Log.e("","Created a file with content: " + Id);

            }
        };

Then with this id in another method i call the file and read it (If i want i can edit this file information from Google Drive Web App):

 public void leer(){
       DriveFile file = Drive.DriveApi.getFile(getGoogleApiClient(),Id);
       file.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null)
               .setResultCallback(contentsOpenedCallback);
   }
ResultCallback<DriveApi.DriveContentsResult> contentsOpenedCallback =
        new ResultCallback<DriveApi.DriveContentsResult>() {
            @Override
            public void onResult(DriveApi.DriveContentsResult result) {
                if (!result.getStatus().isSuccess()) {
                    Log.e("Error:","No se puede abrir el archivo o no se encuentra");
                    return;
                }
                // DriveContents object contains pointers
                // to the actual byte stream
                DriveContents contents = result.getDriveContents();
                BufferedReader reader = new BufferedReader(new InputStreamReader(contents.getInputStream()));
                StringBuilder builder = new StringBuilder();
                String line;
                try {
                    while ((line = reader.readLine()) != null) {
                        builder.append(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String contentsAsString = builder.toString();
                Log.e("RESULT:",contentsAsString);
            }
        };
seanpj

I've been playing with this stuff a few months back, and still have some code on github. It may be VERY outdated (libver 15 or so), but it may serve as a reference point, and it is (I hope simple). Look here. Pull it down, plug in, step through. Fix what's not working anymore :-). I've abandoned it some time ago.

Be aware of the fact that there are 2 different IDs for Google Drive Android API objects, see SO 22841237.

In general, you usually start with knowing the file/folder name, query GDAA to get a list of objects. Each of them will yield DriveID and ResourceID. DriveID is used in your app to manipulate the objects (does not mean anything outside your Android App and/or device). ResourceID is the string that appears in different forms in URLs and can be used outside your app (web browser for instance...). Look at this wrapper to get some feeling how it works. But again, it's been a few versions back, so there are no guaranties. Good Luck...

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