如何使用谷歌驱动器api为Android java创建一个空的spreadheeet

时间:2015-06-23 00:19:01

标签: android google-drive-api

您好我正在尝试使用我的Android应用中的驱动器api创建一个空电子表格 我用mimetype text / *创建文件没问题 但使用“application / vnd.google-apps.spreadsheet”我收到错误消息。 这是我的活动。 我怎样才能达到效果?

import android.app.Activity;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi.DriveContentsResult;
import com.google.android.gms.drive.DriveFolder;
import com.google.android.gms.drive.MetadataChangeSet;

public class SpreadCreate extends Activity implements ConnectionCallbacks, OnConnectionFailedListener {
    private static final int REQUEST_CODE_RESOLUTION = 1;
    private GoogleApiClient mGoogleApiClient;

    private void saveFileToDrive() {
        Drive.DriveApi.newDriveContents(mGoogleApiClient)
                .setResultCallback(new ResultCallback<DriveContentsResult>() {

                    @Override
                    public void onResult(DriveContentsResult result) {

                        if (!result.getStatus().isSuccess()) {
                            return;
                        }
                        new Thread() {
                            @Override
                            public void run() {
                                MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                                        .setTitle("MoneyManager")
                                                //.setMimeType("application/vnd.google-apps.spreadsheet")
                                        .setMimeType("text/csv")
                                        .build();
                                Drive.DriveApi.getRootFolder(mGoogleApiClient)
                                        .createFile(mGoogleApiClient, changeSet, null)
                                        .setResultCallback(fileCallback);

                            }
                        }.start();
                    }
                });
    }

    final private ResultCallback<DriveFolder.DriveFileResult> fileCallback = new
            ResultCallback<DriveFolder.DriveFileResult>() {
                @Override
                public void onResult(DriveFolder.DriveFileResult result) {
                    if (!result.getStatus().isSuccess()) {
                        System.out.println("Errore " + result.toString());
                        finish();
                    }
                    System.out.println(result.getDriveFile().getDriveId());
                    finish();
                }
            };

    @Override
    protected void onResume() {
        super.onResume();
        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    //.addApiIfAvailable(Drive.API)
                    .addApi(Drive.API)
                    .addScope(Drive.SCOPE_FILE)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                            //.setAccountName(accountname)
                    .build();
        }
        mGoogleApiClient.connect();
    }

    @Override
    protected void onPause() {
        if (mGoogleApiClient != null) {
            mGoogleApiClient.disconnect();
        }
        super.onPause();
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        if (!result.hasResolution()) {
            GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
            return;
        }
        try {
            result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
        } catch (SendIntentException e) {
        }
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        saveFileToDrive();
    }

    @Override
    public void onConnectionSuspended(int cause) {
    }
}

由于

0 个答案:

没有答案