安卓:我怎样才能访问A S preadsheet与谷歌驱动器,SDK? [英] Android: How can I access a spreadsheet with the google-drive-sdk?

查看:162
本文介绍了安卓:我怎样才能访问A S preadsheet与谷歌驱动器,SDK?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的Andr​​oid应用程序,它应该是能够读取从谷歌驱动器A S preadsheet。不过,每次当我尝试打开从谷歌驱动器的文件我获得以下从DriveContentsResult消息时间:没有内容可用于该文件

下面是我的code,我得到了谷歌的授权,通过GoogleApiClient驱动器后:

 私有静态最终诠释REQUEST_RESOLVE_ERROR = 1001
公共静态最终诠释REQUEST_ code_OPENER = 1002中;
私人驱动器Id mSelectedFileDriveId;
公共GoogleApiClient mGoogleApiClient;公共无效getGoogleDriveFile(查看视图){
    如果(mGoogleApiClient.isConnected()){
        IntentSender intentSender = Drive.DriveApi
                .newOpenFileActivityBuilder()
                .setMimeType(新的String [] {应用程序/ vnd.google-apps.s preadsheet})
                .build(mGoogleApiClient);        尝试{
            startIntentSenderForResult(intentSender,REQUEST_ code_OPENER,NULL,0,0,0);
        }赶上(SendIntentException E){
            Log.d(TAG,无法发送意图+ E);
        }
    }
}@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    Log.d(TAG的onActivityResult);
    如果(要求code == REQUEST_RESOLVE_ERROR){
     ...
    }否则如果(要求code == REQUEST_ code_OPENER){
        如果(结果code == RESULT_OK){
            mSelectedFileDriveId =(驱动器Id)data.getParcelableExtra(OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
            如果(mSelectedFileDriveId!= NULL){
                打开();
            }
        }
    }
}
私人无效的open(){
     Drive.DriveApi.getFile(mGoogleApiClient,mSelectedFileDriveId)
     。开(mGoogleApiClient,DriveFile.MODE_READ_ONLY,NULL)
     .setResultCallback(driveContentsCallback);
}
私人ResultCallback< D​​riveContentsResult> driveContentsCallback =
     新ResultCallback< D​​riveContentsResult>(){
     @覆盖
     公共无效onResult(DriveContentsResult结果){
         如果(!result.getStatus()。isSuccess()){
             Log.d(TAG,错误打开文件内容+ result.getStatus()getStatusMessage());
             返回;
         }
            DriveContents内容= result.getDriveContents();
        }
};


解决方案

下面的内容指的是二进制内容。谷歌小号preadsheets(及其他谷歌文档类型)不存储为二进制内容。具体Android的API目前不支持读取US preadsheet内容。

使用REST API,你可以得到它像使用ExportLinks CSV二进制格式。

I am working on an android app, which should be able to read a spreadsheet from google drive. However, every time when I try to open a file from google drive I get following message from the DriveContentsResult: "No content is available for this file".

Here is my code, after I got the authorization for google drive via the 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();
        }
};

解决方案

Content here refers to binary content. Google Spreadsheets (and the other Google Docs types) are not stored as binary content. The Android-specific API doesn't currently support reading Spreadsheet contents.

Using the RESTful API, you can get it in a binary format like csv using the ExportLinks.

这篇关于安卓:我怎样才能访问A S preadsheet与谷歌驱动器,SDK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆