I’m trying to retrieve the contents of files stored in Google Drive using the Drive API. My code works fine for regular files like MS Word documents and text files. But I’m running into issues when dealing with Google’s native formats (Docs, Sheets, etc.).
Here’s a snippet of what I’m working with:
fileInDrive.open(apiClient, DriveFile.MODE_READ_ONLY, null)
.setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(DriveApi.DriveContentsResult outcome) {
if (!outcome.getStatus().isSuccess()) {
Log.e(TAG, "Failed to access file content");
return;
}
DriveContents fileContent = outcome.getDriveContents();
BufferedReader contentReader = new BufferedReader(
new InputStreamReader(fileContent.getInputStream())
);
StringBuilder contentBuilder = new StringBuilder();
String textLine;
try {
while ((textLine = contentReader.readLine()) != null) {
contentBuilder.append(textLine);
}
} catch (IOException e) {
e.printStackTrace();
}
String fullContent = contentBuilder.toString();
fileContent.discard(apiClient);
Log.i(TAG, fullContent);
}
});
The code fails for Google format files, giving an unsuccessful result. What’s the proper way to handle Google Docs, Sheets, and other native formats? Do they require special treatment? Any guidance would be appreciated!