我刚刚在此链接上设置了适用于 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): }
有趣的是我能够进行大约 2 次 OCR,然后我可能已经修改了源或其他东西,但现在它无法正常工作。但我记得我必须对快速入门应用程序进行的唯一更改就是将这一行: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();
}
});
}
}