问题描述
登录gdrive后,我尝试以编程方式上传csv文件。
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();
}
我仅连接到gdrive帐户后才尝试上传文件。但它显示服务错误。
我也无法使用云端硬盘服务进行初始化,因为我以gms方式导入了云端硬盘
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;
推荐答案
,包括使用,以允许用户准确地选择要在Google云端硬盘上创建文件的位置或以编程方式调用在。检索适当的 DriveFolder
的方法包括:
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().
这篇关于使用Android API以编程方式将文件上传到Googledrive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!