Upload a file to Googledrive programmatically using the Android API

By going to gdrive, I tried to download the csv file programmatically. But it gives an 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(); } 

After connecting only to the gdrive account, I tried to upload the file. But this shows a maintenance error. I cannot use the Drive service to initialize either, because I imported the drive as 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; 
0
source share
1 answer

There are several methods for creating a Google Drive file , including using CreateFileActivityBuilder so that the user can determine exactly where on their Google Drive they would like to create the file or programmatically call createFile () on the DriveFolder that you have. Ways to get the appropriate DriveFolder include:

0
source

Source: https://habr.com/ru/post/979332/


All Articles