I'm currently working on an app where the user can pick a Google Sheet file (.xlxs) from their Google Drive. My app will then extract and work with certain content of that Sheet.
I'm using the Google Drive API made for Android and I utilize the example classes from Google Drive Android Demos
So far, I've implemented a Drive file picker where the user's Drive files are showing. Picking a file returns the ID for that file.
The first problem is that I can't seem to set the mime type so only xlsl files are pickable. Google searches tell me that
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
is the correct mime type.
I use it in this context
IntentSender intentSender = Drive.DriveApi
.newOpenFileActivityBuilder()
.setMimeType(new String[]{"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"})
.build(getGoogleApiClient());
I haven't encountered mime types before, but "text/plain" as a meme type seems to work, and they are found in the same lists of mime types out there..
The second problem basically concerns how I should go about extract Strings from certain columns and rows in my sheet.
This is the code that deals with the contents of the chosen file (may be modified of course, just don't know how).
protected String doInBackgroundConnected(DriveId... params) {
String contents = null;
DriveFile file = params[0].asDriveFile();
DriveContentsResult driveContentsResult =
file.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null).await();
if (!driveContentsResult.getStatus().isSuccess()) {
return null;
}
DriveContents driveContents = driveContentsResult.getDriveContents();
BufferedReader reader = new BufferedReader(
new InputStreamReader(driveContents.getInputStream()));
StringBuilder builder = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
builder.append(line);
}
contents = builder.toString();
} catch (IOException e) {
Log.e(TAG, "IOException while reading from the stream", e);
}
driveContents.discard(getGoogleApiClient());
return contents;
}