0

I am trying to use Google Drive Android API to read a file from Google drive, and I was trying to understand the example Google gives below. What I am confused about is that what is Drveid and EXISTING_FILE_ID? How do I get them? If I want to open a file by its name, how do I do that? Thanks!

public class EditContentsActivity extends BaseDemoActivity {

private static final String TAG = "EditContentsActivity";

@Override
public void onConnected(Bundle connectionHint) {
    super.onConnected(connectionHint);

    final ResultCallback<DriveIdResult> idCallback = new ResultCallback<DriveIdResult>() {
        @Override
        public void onResult(DriveIdResult result) {
            if (!result.getStatus().isSuccess()) {
                showMessage("Cannot find DriveId. Are you authorized to view this file?");
                return;
            }
            DriveId driveId = result.getDriveId();
            DriveFile file = driveId.asDriveFile();
            new EditContentsAsyncTask(EditContentsActivity.this).execute(file);

        }
    };
    Drive.DriveApi.fetchDriveId(getGoogleApiClient(), EXISTING_FILE_ID)
          .setResultCallback(idCallback);
}

public class EditContentsAsyncTask extends ApiClientAsyncTask<DriveFile, Void, Boolean> {

    public EditContentsAsyncTask(Context context) {
        super(context);
    }

    @Override
    protected Boolean doInBackgroundConnected(DriveFile... args) {
        DriveFile file = args[0];
        try {
            DriveContentsResult driveContentsResult = file.open(
                    getGoogleApiClient(), DriveFile.MODE_WRITE_ONLY, null).await();
            if (!driveContentsResult.getStatus().isSuccess()) {
                return false;
            }
            DriveContents driveContents = driveContentsResult.getDriveContents();
            OutputStream outputStream = driveContents.getOutputStream();
            outputStream.write("Hello world".getBytes());
            com.google.android.gms.common.api.Status status =
                    driveContents.commit(getGoogleApiClient(), null).await();
            return status.getStatus().isSuccess();
        } catch (IOException e) {
            Log.e(TAG, "IOException while appending to the output stream", e);
        }
        return false;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if (!result) {
            showMessage("Error while editing contents");
            return;
        }
        showMessage("Successfully edited contents");
    }
}
x1uan
  • 153
  • 1
  • 2
  • 8
  • DriveId is prolly a model object which is found in the Google APIs for Android under [DriveId](https://developers.google.com/android/reference/com/google/android/gms/drive/DriveId). EXISTING_FILE_ID is the real id of the file in your Google Drive which is a bunch of alphanumeric characters. – ReyAnthonyRenacia May 03 '17 at 16:30
  • @noogui is the file id generated by the API or I get to pick? Can I use file id to locate specific file I need? – x1uan May 03 '17 at 16:37

2 Answers2

0
  • DriveId is defined here as: A canonical identifier for a Drive resource. The identifier can be converted to a String representation for storage using encodeToString() and then later converted back to the ID representation using decodeFromString(String). equals(Object) can be used to see if two different identifiers refer to the same resource.

  • EXISTING_FILE_ID in Google's drive api demo code refers to the saved file id of an existing file in drive. file Id is auto-generated.

check this SO link for more on how to get driveId/file id to use that file

You can get a file by its name but its better to use driveId as its unique.

Community
  • 1
  • 1
AndroidLearner
  • 741
  • 6
  • 11
  • If were to retrieve an file's content and all I know if file name, how do I get the file id? – x1uan May 05 '17 at 21:40
  • Query for files or list a folders content as documented in the [Google drive APIs for android - **Search for files** and **Folders** section](https://developers.google.com/drive/android/queries) – AndroidLearner May 06 '17 at 03:45
  • If u think my answer helped you, pls accept it as the best answer. – AndroidLearner May 08 '17 at 07:44
0

Ok, here is how I did it. FileID is auto-generated when file was created. Once we have fileId. Below can be used to get the file.

    Drive.DriveApi.fetchDriveId(getGoogleApiClient(), fileId).setResultCallback(idCallback);

Query can be used to get the FileId of files.

    Query query = new Query.Builder().addFilter(Filters.eq(SearchableField.TITLE, fileName)).build();
    Drive.DriveApi.query(getGoogleApiClient(), query).setResultCallback(metadataCallback);

At least the codes above were what Google did in its demo app.

x1uan
  • 153
  • 1
  • 2
  • 8