Android:如何使用google-drive-sdk访问电子表格?

时间:2014-10-12 23:26:13

标签: android google-drive-api

我正在开发一个Android应用,它应该能够从谷歌驱动器中读取电子表格。但是,每当我尝试从谷歌驱动器打开文件时,我都会收到来自DriveContentsResult的消息:"此文件没有可用的内容"。

这是我的代码,在我通过GoogleApiClient获得谷歌驱动器的授权后:

private static final int REQUEST_RESOLVE_ERROR = 1001;
public static final int REQUEST_CODE_OPENER = 1002;
private DriveId mSelectedFileDriveId;
public GoogleApiClient mGoogleApiClient;

public void getGoogleDriveFile(View view){
    if(mGoogleApiClient.isConnected()){
        IntentSender intentSender = Drive.DriveApi
                .newOpenFileActivityBuilder()
                .setMimeType(new String[] {"application/vnd.google-apps.spreadsheet"})
                .build(mGoogleApiClient);

        try {
            startIntentSenderForResult(intentSender, REQUEST_CODE_OPENER, null, 0, 0, 0);
        } catch (SendIntentException e) {
            Log.d(TAG, "Unable to send intent" + e);
        }
    } 
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityResult");
    if (requestCode == REQUEST_RESOLVE_ERROR) {
     ...
    } else if(requestCode == REQUEST_CODE_OPENER){
        if(resultCode == RESULT_OK){
            mSelectedFileDriveId = (DriveId) data.getParcelableExtra(OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);                          
            if(mSelectedFileDriveId != null){
                open();
            }  
        } 
    }
}
private void open() {
     Drive.DriveApi.getFile(mGoogleApiClient, mSelectedFileDriveId)
     .open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null)
     .setResultCallback(driveContentsCallback);
}
private ResultCallback<DriveContentsResult> driveContentsCallback =
     new ResultCallback<DriveContentsResult>() {
     @Override
     public void onResult(DriveContentsResult result) {
         if (!result.getStatus().isSuccess()) {
             Log.d(TAG, "Error while opening the file contents " + result.getStatus().getStatusMessage());
             return;
         }
            DriveContents contents = result.getDriveContents();
        }
};

2 个答案:

答案 0 :(得分:1)

此处的内容是指二进制内容。 Google Spreadsheets(以及其他Google Docs类型)不会存储为二进制内容。特定于Android的API目前不支持阅读电子表格内容。

使用RESTful API,您可以使用ExportLinks以二进制格式(如csv)获取它。

答案 1 :(得分:0)

使用&#34; google电子表格api&#34; (REST)没有驱动api。