【问题标题】:Upload file to Googledrive programmatically using Android API使用 Android API 以编程方式将文件上传到 Google Drive
【发布时间】:2015-01-02 06:07:49
【问题描述】:

登录 gdrive 后,我尝试以编程方式上传 csv 文件。但它会在服务中引发错误。

public class Gdriveactivity extends Activity implements ConnectionCallbacks,
OnConnectionFailedListener {

private static final String TAG = "android-drive-quickstart";
    private static final int REQUEST_CODE_CAPTURE_IMAGE = 1;
    private static final int REQUEST_CODE_CREATOR = 2;
    private static final int REQUEST_CODE_RESOLUTION = 3;

    private GoogleApiClient mGoogleApiClient;
    private Bitmap mBitmapToSave;

    private void saveFiletoDrive() {

        ResultCallback<DriveApi.ContentsResult> contentsCallback = new
                ResultCallback<DriveApi.ContentsResult>() {
            @Override
            public void onResult(DriveApi.ContentsResult result) {

                    MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                    .setTitle("New file")
                    .setMimeType("text/plain").build();


                    Drive.DriveApi.getRootFolder(mGoogleApiClient)
                    .createFile(mGoogleApiClient, changeSet, null /* DriveContents */)
                    .setResultCallback(this);
                }
            };
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mGoogleApiClient == null) {
            // Create the API client and bind it to an instance variable.
            // We use this instance as the callback for connection and connection
            // failures.
            // Since no account name is passed, the user is prompted to choose.
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        }
        // Connect the client. Once connected, the camera is launched.
        mGoogleApiClient.connect();
    }

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

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        switch (requestCode) {
            case REQUEST_CODE_CAPTURE_IMAGE:
                // Called after a photo has been taken.
                if (resultCode == Activity.RESULT_OK) {
                    // Store the image data as a bitmap for writing later.
                    mBitmapToSave = (Bitmap) data.getExtras().get("data");
                }
                break;
            case REQUEST_CODE_CREATOR:
                // Called after a file is saved to Drive.
                if (resultCode == RESULT_OK) {
                    Log.i(TAG, "Image successfully saved.");
                    mBitmapToSave = null;
                    // Just start the camera again for another photo.
                    startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
                            REQUEST_CODE_CAPTURE_IMAGE);

                }
                break;
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // Called whenever the API client fails to connect.
        Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
        Toast.makeText(Gdriveactivity.this, "connected failed", 1000).show();
        if (!result.hasResolution()) {
            // show the localized error dialog.
            GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
            return;
        }

        // Called typically when the app is not yet authorized, 
        // authorization
        // dialog is displayed to the user.
        try {
            result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
        } catch (SendIntentException e) {
            Log.e(TAG, "Exception while starting resolution activity", e);
        }
    }
    @Override
    public void onConnected(Bundle connectionHint) {
        Log.i(TAG, "API client connected.");
        if (mBitmapToSave == null) {
            // This activity has no UI of its own. Just start the camera.
            Toast.makeText(Gdriveactivity.this, "connected", 1000).show();
            //startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
            //   REQUEST_CODE_CAPTURE_IMAGE);
            //   return;
        }
        insertFile();
    }

连接到 gdrive 帐户后,我只尝试上传文件。但它显示服务错误。 我也无法使用 Drive 服务进行初始化,因为我以 gms 方式导入了驱动器

import com.google.android.gms.drive.Drive;
import com.google.api.client.http.FileContent;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.ParentReference;

【问题讨论】:

  • @ianhanniballake 我无法使用服务它在这里显示错误 File file = service.files().insert(body, mediaContent).execute();服务无法解决。
  • 那么错误信息是什么?
  • 导入 com.google.api.services.drive.Drive;我不能使用这个导入功能。我可以像这样声明驱动器服务,但是如何在这里初始化驱动器。如果我这样做了,它会显示这样的错误驱动器类型的方法 files() 是未定义的
  • @ianhanniballake 这是我插入文件的方法不对吗??
  • “服务”在哪里定义?

标签: android csv google-drive-api google-drive-android-api


【解决方案1】:

Creating a Google Drive File 有多种方法,包括使用 CreateFileActivityBuilder 允许用户准确选择他们想要在 Google 云端硬盘上创建文件的位置,或以编程方式在 DriveFolder 上调用 createFile()有。检索适当的DriveFolder 的方法包括:

【讨论】:

  • 这是我需要编写任何额外的类来存储身份验证的东西吗,bcz 一旦登录我需要以编程方式而不是手动上传文件/csv
  • 一旦您登录一次,您可以在没有任何用户干预(即从后台服务)的情况下再次登录,前提是用户没有手动删除对您应用的访问权限。您可能确实希望通过getDriveId().encodeToString() 保存创建的DriveFile 的驱动器ID - 这将允许您非常轻松地更新文件。
  • 你看到我上面的代码了吗..第一次只有我登录,下一次它不询问用户 intrevention.is 这行代码是插入文件的正确方法 File file = service.files() .insert(body, mediaContent).execute();
  • 您的登录代码看起来不错,但您需要按照我的回答使用createFile() 方法来创建文件。
  • 在此代码中 ResultCallback contentsCallback = new ResultCallback() { public void onResult(DriveContentsResult result) { 是 DriveContentResult 给出错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-10
  • 1970-01-01
相关资源
最近更新 更多