Upload file to Googledrive programmatically using Android API

£可爱£侵袭症+ 提交于 2019-12-12 03:18:12

问题


Once I logged in gdrive I tried to upload csv file programmatically. But it throws error in service.

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

Once I connected to gdrive account only I tried to upload file. But it shows error in service. I can't use Drive service to initialize also because I imported drive in gms way

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;

回答1:


There are a number of methods of Creating a Google Drive File including using a CreateFileActivityBuilder to allow the user to pick exactly where on their Google Drive they would like to create the file or programmatically calling createFile() on a DriveFolder you have. Ways of retrieving the appropriate DriveFolder include:

  • Drive.DriveApi.getRootFolder() (for the public root directory)
  • Drive.DriveApi.getAppFolder() (for your application private directory - the user cannot see these files, only how much storage space they take)
  • through a result of Drive.DriveApi.query().


来源:https://stackoverflow.com/questions/27738070/upload-file-to-googledrive-programmatically-using-android-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!