问题描述
在我的GDrive登录我尝试上传CSV文件programmatically.But它服务引发的错误。
公共类Gdriveactivity扩展活动实现ConnectionCallbacks,
OnConnectionFailedListener {私有静态最后弦乐TAG =Android的驱动器快速入门;
私有静态最终诠释REQUEST_ code_CAPTURE_IMAGE = 1;
私有静态最终诠释REQUEST_ code_CREATOR = 2;
私有静态最终诠释REQUEST_ code_RESOLUTION = 3;私人GoogleApiClient mGoogleApiClient;
私人位图mBitmapToSave; 私人无效saveFiletoDrive(){ ResultCallback< DriveApi.ContentsResult> contentsCallback =新
ResultCallback< DriveApi.ContentsResult>(){
@覆盖
公共无效onResult(DriveApi.ContentsResult结果){ MetadataChangeSet变更=新MetadataChangeSet.Builder()
.setTitle(新文件)
.setMimeType(text / plain的),建立()。
Drive.DriveApi.getRootFolder(mGoogleApiClient)
.createFile(mGoogleApiClient,变更,空/ * * DriveContents /)
.setResultCallback(本); }
};
}@覆盖
保护无效onResume(){
super.onResume();
如果(mGoogleApiClient == NULL){
//创建API客户,并将其绑定到一个实例变量。
//我们用这个实例作为回调连接和连接
//失败。
//由于没有帐户名通过后,系统会提示用户选择。
mGoogleApiClient =新GoogleApiClient.Builder(本)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(本)
.addOnConnectionFailedListener(本)
。建立();
}
//连接客户端。一旦连接,相机启动。
mGoogleApiClient.connect();
}@覆盖
保护无效的onPause(){
如果(mGoogleApiClient!= NULL){
mGoogleApiClient.disconnect();
}
super.onPause();
}@覆盖
保护无效的onActivityResult(最终诠释请求code,最终诠释结果code,最终意向数据){
开关(要求code){
案例REQUEST_ code_CAPTURE_IMAGE:
//调用照片拍摄后。
如果(结果code == Activity.RESULT_OK){
//存储图像数据作为位图后写作。
。mBitmapToSave =(位图)data.getExtras()获得(数据);
}
打破;
案例REQUEST_ code_CREATOR:
//调用一个文件被保存到硬盘后。
如果(结果code == RESULT_OK){
Log.i(TAG,图像保存成功。);
mBitmapToSave = NULL;
//刚刚再次启动相机,另一张照片。
startActivityForResult(新意图(MediaStore.ACTION_IMAGE_CAPTURE)
REQUEST_ code_CAPTURE_IMAGE); }
打破;
}
}@覆盖
公共无效onConnectionFailed(ConnectionResult结果){
//每当API客户端无法连接调用。
Log.i(TAGGoogleApiClient连接失败:+ result.toString());
Toast.makeText(Gdriveactivity.this,连接失败,1000).show();
如果(!result.hasResolution()){
//显示本地化错误对话框。
GooglePlayServicesUtil.getErrorDialog(result.getError code(),这一点,0).show();
返回;
}//调用通常当应用程序尚未授权,
//授权
//对话框被显示给用户。
尝试{
result.startResolutionForResult(这一点,REQUEST_ code_RESOLUTION);
}赶上(SendIntentException E){
Log.e(TAG,异常而开始活动的决议,E);
}
}
@覆盖
公共无效onConnected(捆绑connectionHint){
Log.i(TAG,API客户端连接。);
如果(mBitmapToSave == NULL){
//这个活动有没有自己的UI。刚开始的相机。
Toast.makeText(Gdriveactivity.this,连接,1000).show();
// startActivityForResult(新意图(MediaStore.ACTION_IMAGE_CAPTURE)
// REQUEST_ code_CAPTURE_IMAGE);
//返回;}
中的insertFile();
}
在我连接到GDrive的帐户只我试图上传file.But它显示了错误的服务。
我不能使用驱动器服务还初始化,因为我在大湄公河次区域方式进口传动
进口com.google.android.gms.drive.Drive;
进口com.google.api.client.http.FileContent;
进口com.google.api.services.drive.model.File;
进口com.google.api.services.drive.model.ParentReference;
有许多的包括使用 CreateFileActivityBuilder 以允许用户挑选正是他们的谷歌驱动器,他们希望创建文件或以编程调用的CreateFile() > DRIVEFOLDER 你。检索相应的 DRIVEFOLDER
办法包括:
- (用于公共的根目录)
- (您的应用程序私有目录 - 用户不能看到这些文件,它们只是多少存储空间取)
- 通过 Drive.DriveApi.query()。
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 cant 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;
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().
这篇关于上传文件到Google云端硬盘progrmmatically采用Android API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!