我正在尝试将我的应用程序连接到谷歌驱动器。所以首先我尝试了示例。我按照谷歌驱动器教程进行操作。首先它要求选择帐户并允许通过相机拍照,然后什么都没有发生(只是白屏)。没有烤面包显示。我调试了,它停在
File file = service.files().insert(body, mediaContent).execute();
在这里,我给出了我的样本
public class MainActivity extends Activity {
static final int REQUEST_ACCOUNT_PICKER = 1;
static final int REQUEST_AUTHORIZATION = 2;
static final int CAPTURE_IMAGE = 3;
public static File f;
private static Uri fileUri;
private static Drive service;
private GoogleAccountCredential credential;
private Bitmap mPhoto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@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) {
getContentResolver().notifyChange(fileUri, null);
ContentResolver cr = getContentResolver();
try {
mPhoto = android.provider.MediaStore.Images.Media
.getBitmap(cr, fileUri);
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT)
.show();
}
saveFileToDrive();
}
}
}
private void startCameraIntent() {
Intent c = new Intent("android.media.action.IMAGE_CAPTURE");
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
java.io.File f = new java.io.File(Environment.getExternalStorageDirectory(),
"abc-" + timeStamp
+ ".jpg");
Log.e("FILE", f.getAbsolutePath());
c.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
fileUri = Uri.fromFile(f);
startActivityForResult(c, CAPTURE_IMAGE);
}
private void saveFileToDrive() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
// File's binary content
java.io.File fileContent = new java.io.File(fileUri.getPath());
FileContent mediaContent = new FileContent("image/jpeg", fileContent);
// File's 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();
}
});
}
}
停在service.files().insert(body, mediaContent).execute(); 并收到警告,因为vfy 无法解析静态字段没有错误,但什么也没发生。请提供任何建议..
logcat 堆栈跟踪如下
08-05 14:38:07.029: W/KeyCharacterMap(10792): Can't open keycharmap file
08-05 14:38:07.029: W/KeyCharacterMap(10792): Error loading keycharmap file
08-05 14:38:07.029: W/KeyCharacterMap(10792): Using default keymap
08-05 14:38:27.989: W/AbstractGoogleClient(10962): Application name is not set. Call Builder#setApplicationName.
08-05 14:38:42.729: W/dalvikvm(10962): VFY: unable to resolve static field 685 (auth_client_play_services_err_notification_msg) in Lcom/google/android/gms/R$string;
08-05 14:38:42.879: W/dalvikvm(10962): VFY: unable to resolve static field 694 (common_google_play_services_install_title) in Lcom/google/android/gms/R$string;
08-05 14:38:42.879: W/dalvikvm(10962): VFY: unable to resolve static field 690 (common_google_play_services_enable_title) in Lcom/google/android/gms/R$string;
08-05 14:38:42.889: W/dalvikvm(10962): VFY: unable to resolve static field 700 (common_google_play_services_update_title) in Lcom/google/android/gms/R$string;
08-05 14:38:42.889: W/dalvikvm(10962): VFY: unable to resolve static field 697 (common_google_play_services_unsupported_title) in Lcom/google/android/gms/R$string;
08-05 14:38:42.889: W/dalvikvm(10962): VFY: unable to resolve static field 691 (common_google_play_services_install_button) in Lcom/google/android/gms/R$string;
08-05 14:38:42.899: W/dalvikvm(10962): VFY: unable to resolve static field 688 (common_google_play_services_enable_button) in Lcom/google/android/gms/R$string;
08-05 14:38:42.899: W/dalvikvm(10962): VFY: unable to resolve static field 698 (common_google_play_services_update_button) in Lcom/google/android/gms/R$string;
08-05 14:38:42.899: W/dalvikvm(10962): VFY: unable to resolve static field 695 (common_google_play_services_unknown_issue) in Lcom/google/android/gms/R$string;
08-05 14:38:42.899: W/dalvikvm(10962): VFY: unable to resolve instance field 10