在以下代码中,我尝试将相机拍摄的图像上传到 Google Drive。我没有使用帐户选择器来选择帐户名称,但我正在通过程序本身传递帐户名称。我正在调用一个 handleCamera 来启动相机并捕获图像,并使用 save2Drive() 将其上传到 GoogleDrive。代码是
public class first extends Activity {
static final int CAPTURE_IMAGE = 3;
private static Uri fileUri = null;
private static Drive service = null;
private GoogleAccountCredential credential = null;
String accountName = "blogmsr28@gmail.com";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.firstscreen);
credential = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(new String[]{DriveScopes.DRIVE}));
credential.setSelectedAccountName(accountName);
service = getDriveService(credential);
showToast(" Inside oncreate");
handleCamera();
save2Drive();
}
public void handleCamera(){
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);
}
public void save2Drive() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
java.io.File fileContent = new java.io.File(fileUri.getPath());
FileContent mediaContent = new FileContent("image/jpeg", fileContent);
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("image/jpeg");
File file = service.files().insert(body, mediaContent).setOcr(true).execute();
if (file != null) {
showToast("Photo uploaded: " + file.getTitle());
}
} catch (UserRecoverableAuthIOException e) {
e.printStackTrace();
} 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_LONG).show();
}
});
}
}
堆栈跟踪是
05-10 14:48:53.074: E/GooglePlayServicesUtil(17592): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
05-10 14:48:53.148: E/AndroidRuntime(17592): FATAL EXCEPTION: Thread-11750
05-10 14:48:53.148: E/AndroidRuntime(17592): Process: com.example.drivequickstart, PID: 17592
05-10 14:48:53.148: E/AndroidRuntime(17592): java.lang.IllegalArgumentException: the name must not be empty: null
05-10 14:48:53.148: E/AndroidRuntime(17592): at android.os.Parcel.readException(Parcel.java:1624)
05-10 14:48:53.148: E/AndroidRuntime(17592): at android.os.Parcel.readException(Parcel.java:1573)
05-10 14:48:53.148: E/AndroidRuntime(17592): at com.google.android.gms.internal.o$a$a.a(Unknown Source)
05-10 14:48:53.148: E/AndroidRuntime(17592): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
05-10 14:48:53.148: E/AndroidRuntime(17592): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
05-10 14:48:53.148: E/AndroidRuntime(17592): at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:255)
05-10 14:48:53.148: E/AndroidRuntime(17592): at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:279)
05-10 14:48:53.148: E/AndroidRuntime(17592): at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:858)
05-10 14:48:53.148: E/AndroidRuntime(17592): at com.google.api.client.googleapis.media.MediaHttpUploader.executeCurrentRequest(MediaHttpUploader.java:559)
05-10 14:48:53.148: E/AndroidRuntime(17592): at com.google.api.client.googleapis.media.MediaHttpUploader.executeCurrentRequestWithBackOffAndGZip(MediaHttpUploader.java:581)
05-10 14:48:53.148: E/AndroidRuntime(17592): at com.google.api.client.googleapis.media.MediaHttpUploader.executeUploadInitiation(MediaHttpUploader.java:533)
05-10 14:48:53.148: E/AndroidRuntime(17592): at com.google.api.client.googleapis.media.MediaHttpUploader.resumableUpload(MediaHttpUploader.java:393)
05-10 14:48:53.148: E/AndroidRuntime(17592): at com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:345)