Search code examples
androidandroid-studiogoogle-drive-android-api

How to open a specific file using Google Drive Android API?


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

Solution

    • 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.