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

标签 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();
        }
};

最佳答案

这里的内容是指二进制内容。 Google 电子表格(和其他 Google 文档类型)不存储为二进制内容。 Android 特定的 API 目前不支持读取电子表格内容。

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

关于Android:如何使用 google-drive-sdk 访问电子表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26331046/

相关文章:

android - 为什么我的 webview 中的 youtube 只播放声音?

Android Studio - 在 fragment 中使用 Assets 管理器设置自定义字体

google-drive-api - 如何使用 Google Drive 作为一种 CDN

javascript - Google Drive API 文件 downloadUrl 返回 401

android - 仅允许 Android 应用程序访问 Rails API 的最佳实践

android - 以编程方式创建 TextView 后更改 TextView 字体

android - GridView 不均匀地拉伸(stretch) ImageView

google-drive-api - 无法在 Google API 的 Drive UI 集成中保存任何内容

asp.net-core - Google Drive Api 错误 403 无法使用服务帐户添加父级

google-drive-api - Google drive - WSL2 中的虚拟驱动器挂载