使用 Android 应用程序连接到 google Drive
Connecting to google Drive with Android app
你好,我正在使用打开相机的代码,当你拍照时它会将照片发送到 google 驱动器,但我遇到了两个错误,我不知道如何修复
完整代码如下:
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.accounts.AccountManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Toast;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException;
import com.google.api.client.http.FileContent;
import com.google.api.client.json.gson.GsonFactory
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
public class MainActivity extends Activity{
static final int REQUEST_ACCOUNT_PICKER = 1;
static final int REQUEST_AUTHORIZATION=2;
static final int CAPTURE_IMAGE= 3;
private static Uri fileUri;
private static Drive service;
private GoogleAccountCredential credential;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data){
switch (requestCode){
case REQUEST_ACCOUNT_PICKER:
if(resultCode == RESULT_OK && data != null && data.getExtras() !=null){
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
if(accountName != null){
credential.setSelectedAccountName(accountName);
service = getDriveService(credential);
startCameraIntent();
}
}
break;
case REQUEST_AUTHORIZATION:
if(resultCode == Activity.RESULT_OK){
saveFileToDrive();
}else {
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
}
break;
case CAPTURE_IMAGE:
if(resultCode == Activity.RESULT_OK){
saveFileToDrive();
}
}
}
private void startCameraIntent(){
String mediaStorageDir =Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getPath();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss" , Locale.US).format(new Date());
fileUri = Uri.fromFile(new java.io.File(mediaStorageDir + java.io.File.separator + "IMG_" + timeStamp + ".jpg"));
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);
startActivityForResult(cameraIntent, CAPTURE_IMAGE);
}
private void saveFileToDrive(){
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try{
// Files binary content
java.io.File fileContent= new java.io.File(fileUri.getPath());
FileContent mediaContent = new FileContent("image/jpeg", fileContent);
//Files Metadata
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("image/jpeg");
File file = service.files().insert(body, mediaContent).execute();
if(file != null){
showToast ("Photo uploaded" + file.getTitle());
startCameraIntent();
}
}catch (UserRecoverableAuthIOException e){
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
}catch (IOException e){
e.printStackTrace();
}
}
});
t.start();
}
private Drive getDriveService(GoogleAccountCredential credential){
return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(),credential).build();
}
public void showToast(final String toast){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show();
}
});
}
}//end of main
我有两个错误,在这一行
credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);
它说 GoogleAccountCredential 不能应用于(MainActivity,java.lang.String)
并在这一行
File file = service.files().insert(body, mediaContent).execute();
它说无法解析方法执行
我需要帮助,我一直在尝试 link 我的应用程序到 google 驱动器,但大多数时候都失败了。如果有任何其他方法 link 到 google 驱动器请告诉我谢谢
我已经解决了 OAuth2 行中的第一个错误我将代码更改为:
credential = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(DriveScopes.DRIVE_FILE));
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
对于第二个错误,我刚刚添加了一个名为
的 jar 文件
google-api-services-drive-v2-rev168-1.18.0-rc.jar
你好,我正在使用打开相机的代码,当你拍照时它会将照片发送到 google 驱动器,但我遇到了两个错误,我不知道如何修复
完整代码如下:
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.accounts.AccountManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Toast;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException;
import com.google.api.client.http.FileContent;
import com.google.api.client.json.gson.GsonFactory
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
public class MainActivity extends Activity{
static final int REQUEST_ACCOUNT_PICKER = 1;
static final int REQUEST_AUTHORIZATION=2;
static final int CAPTURE_IMAGE= 3;
private static Uri fileUri;
private static Drive service;
private GoogleAccountCredential credential;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data){
switch (requestCode){
case REQUEST_ACCOUNT_PICKER:
if(resultCode == RESULT_OK && data != null && data.getExtras() !=null){
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
if(accountName != null){
credential.setSelectedAccountName(accountName);
service = getDriveService(credential);
startCameraIntent();
}
}
break;
case REQUEST_AUTHORIZATION:
if(resultCode == Activity.RESULT_OK){
saveFileToDrive();
}else {
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
}
break;
case CAPTURE_IMAGE:
if(resultCode == Activity.RESULT_OK){
saveFileToDrive();
}
}
}
private void startCameraIntent(){
String mediaStorageDir =Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getPath();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss" , Locale.US).format(new Date());
fileUri = Uri.fromFile(new java.io.File(mediaStorageDir + java.io.File.separator + "IMG_" + timeStamp + ".jpg"));
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);
startActivityForResult(cameraIntent, CAPTURE_IMAGE);
}
private void saveFileToDrive(){
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try{
// Files binary content
java.io.File fileContent= new java.io.File(fileUri.getPath());
FileContent mediaContent = new FileContent("image/jpeg", fileContent);
//Files Metadata
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("image/jpeg");
File file = service.files().insert(body, mediaContent).execute();
if(file != null){
showToast ("Photo uploaded" + file.getTitle());
startCameraIntent();
}
}catch (UserRecoverableAuthIOException e){
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
}catch (IOException e){
e.printStackTrace();
}
}
});
t.start();
}
private Drive getDriveService(GoogleAccountCredential credential){
return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(),credential).build();
}
public void showToast(final String toast){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show();
}
});
}
}//end of main
我有两个错误,在这一行
credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);
它说 GoogleAccountCredential 不能应用于(MainActivity,java.lang.String)
并在这一行
File file = service.files().insert(body, mediaContent).execute();
它说无法解析方法执行
我需要帮助,我一直在尝试 link 我的应用程序到 google 驱动器,但大多数时候都失败了。如果有任何其他方法 link 到 google 驱动器请告诉我谢谢
我已经解决了 OAuth2 行中的第一个错误我将代码更改为:
credential = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(DriveScopes.DRIVE_FILE));
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
对于第二个错误,我刚刚添加了一个名为
的 jar 文件google-api-services-drive-v2-rev168-1.18.0-rc.jar