我刚刚在this链接
上设置了适用于Android的快速启动google drive sdk应用程序我正在尝试上传图片,然后对它们进行OCR。 Android快速入门上的示例应用程序运行正常,但是当我尝试将OCR的布尔值设置为true时,我得到以下IOException:
07-29 03:33:01.172: D/OCR_SERVICE(22602): Error 2: 400 Bad Request
07-29 03:33:01.172: D/OCR_SERVICE(22602): {
07-29 03:33:01.172: D/OCR_SERVICE(22602): "code": 400,
07-29 03:33:01.172: D/OCR_SERVICE(22602): "errors": [
07-29 03:33:01.172: D/OCR_SERVICE(22602): {
07-29 03:33:01.172: D/OCR_SERVICE(22602): "domain": "global",
07-29 03:33:01.172: D/OCR_SERVICE(22602): "message": "Bad Request",
07-29 03:33:01.172: D/OCR_SERVICE(22602): "reason": "badRequest"
07-29 03:33:01.172: D/OCR_SERVICE(22602): }
07-29 03:33:01.172: D/OCR_SERVICE(22602): ],
07-29 03:33:01.172: D/OCR_SERVICE(22602): "message": "Bad Request"
07-29 03:33:01.172: D/OCR_SERVICE(22602): }
有趣的是我能够做OCR大约2次,然后我可能已经修改了源或其他东西,现在它不起作用。但我记得我从快速启动应用程序中做出的唯一更改是将此行更改为File file = service.files().insert(body, mediaContent).execute();
以下内容:
File file = service.files().insert(body, mediaContent).setOcr(true).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;
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 {
// 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).setOcr(true).execute();
if (file != null) {
showToast("Photo uploaded: " + file.getTitle());
startCameraIntent();
}
} catch (UserRecoverableAuthIOException e) {
Log.d("OCR_SERVICE", "Error 1: " + e.getMessage());
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (IOException e) {
Log.d("OCR_SERVICE", "Error 2: " + e.getMessage());
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();
}
});
}
}
答案 0 :(得分:2)
我刚从文档工作中完成了此Google Drive Quickstart示例。代码已过时,甚至一开始都没有编译。一旦成功,我在设备上运行应用程序时遇到了一些其他问题。
我已经列出了使其工作并致力于以下github项目所需的更改。这是一个Eclipse ADT项目,所以请随时查看并与您的代码进行比较。我已经测试了启用OCR选项并验证了结果。
https://github.com/hanscappelle/more-android-examples/tree/master/DriveQuickstart
自述文件概述了所有必需的更改。