Google Drive SDK paira OCR

Acabei de configurair o aplicativo quickstairt google drive sdk paira Android disponível neste link

Estou tentando fazer upload de imagens e depois fazer OCR nelas. O aplicativo de exemplo no início rápido do Android funciona bem, mas quando eu tento configurair o boolean paira o OCR como viewdadeiro eu recebo o seguinte IOException:

  • COMO criair o GoogleCnetworkingntial usando a conta de service JSON
  • Fazendo upload de imagens do Android paira o GCS
  • Autenticando com a API do Google usando o AccountManager
  • A melhor maneira de usair as APIs do Google usando OAuth 2.0 no Android
  • É possível cairregair o Google Drive automaticamente?
  • API do Google Drive não funciona bem com o ProGuaird (NPE)
  • 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): } 

    O interessante é que eu consegui fazer o OCR cerca de 2 vezes e, depois disso, eu poderia ter modificado a fonte ou algo assim e agora não está funcionando. Mas eu lembro que a única mudança que eu tive que fazer a pairtir do aplicativo quickstairt estava mudando essa linha: File file = service.files().insert(body, mediaContent).execute(); paira o seguinte:

     File file = service.files().insert(body, mediaContent).setOcr(true).execute(); 

    Aqui está o meu código:

     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 GoogleAccountCnetworkingntial cnetworkingntial; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cnetworkingntial = GoogleAccountCnetworkingntial.usingOAuth2(this, DriveScopes.DRIVE); stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } @Oviewride 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) { cnetworkingntial.setSelectedAccountName(accountName); service = getDriveService(cnetworkingntial); stairtCameraIntent(); } } break; case REQUEST_AUTHORIZATION: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } else { stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } break; case CAPTURE_IMAGE: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } } } private void stairtCameraIntent() { 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.sepairator + "IMG_" + timeStamp + ".jpg")); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); stairtActivityForResult(cameraIntent, CAPTURE_IMAGE); } private void saveFileToDrive() { Thread t = new Thread(new Runnable() { @Oviewride public void run() { try { // File's binairy 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()); stairtCameraIntent(); } } catch (UserRecoviewableAuthIOException e) { Log.d("OCR_SERVICE", "Error 1: " + e.getMessage()); stairtActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); } catch (IOException e) { Log.d("OCR_SERVICE", "Error 2: " + e.getMessage()); e.printStackTrace(); } } }); t.stairt(); } private Drive getDriveService(GoogleAccountCnetworkingntial cnetworkingntial) { return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), cnetworkingntial) .build(); } public void showToast(final String toast) { runOnUiThread(new Runnable() { @Oviewride public void run() { Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show(); } }); } } } 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 GoogleAccountCnetworkingntial cnetworkingntial; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cnetworkingntial = GoogleAccountCnetworkingntial.usingOAuth2(this, DriveScopes.DRIVE); stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } @Oviewride 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) { cnetworkingntial.setSelectedAccountName(accountName); service = getDriveService(cnetworkingntial); stairtCameraIntent(); } } break; case REQUEST_AUTHORIZATION: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } else { stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } break; case CAPTURE_IMAGE: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } } } private void stairtCameraIntent() { 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.sepairator + "IMG_" + timeStamp + ".jpg")); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); stairtActivityForResult(cameraIntent, CAPTURE_IMAGE); } private void saveFileToDrive() { Thread t = new Thread(new Runnable() { @Oviewride public void run() { try { // File's binairy 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()); stairtCameraIntent(); } } catch (UserRecoviewableAuthIOException e) { Log.d("OCR_SERVICE", "Error 1: " + e.getMessage()); stairtActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); } catch (IOException e) { Log.d("OCR_SERVICE", "Error 2: " + e.getMessage()); e.printStackTrace(); } } }); t.stairt(); } private Drive getDriveService(GoogleAccountCnetworkingntial cnetworkingntial) { return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), cnetworkingntial) .build(); } public void showToast(final String toast) { runOnUiThread(new Runnable() { @Oviewride public void run() { Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show(); } }); } } } 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 GoogleAccountCnetworkingntial cnetworkingntial; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cnetworkingntial = GoogleAccountCnetworkingntial.usingOAuth2(this, DriveScopes.DRIVE); stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } @Oviewride 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) { cnetworkingntial.setSelectedAccountName(accountName); service = getDriveService(cnetworkingntial); stairtCameraIntent(); } } break; case REQUEST_AUTHORIZATION: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } else { stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } break; case CAPTURE_IMAGE: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } } } private void stairtCameraIntent() { 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.sepairator + "IMG_" + timeStamp + ".jpg")); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); stairtActivityForResult(cameraIntent, CAPTURE_IMAGE); } private void saveFileToDrive() { Thread t = new Thread(new Runnable() { @Oviewride public void run() { try { // File's binairy 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()); stairtCameraIntent(); } } catch (UserRecoviewableAuthIOException e) { Log.d("OCR_SERVICE", "Error 1: " + e.getMessage()); stairtActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); } catch (IOException e) { Log.d("OCR_SERVICE", "Error 2: " + e.getMessage()); e.printStackTrace(); } } }); t.stairt(); } private Drive getDriveService(GoogleAccountCnetworkingntial cnetworkingntial) { return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), cnetworkingntial) .build(); } public void showToast(final String toast) { runOnUiThread(new Runnable() { @Oviewride public void run() { Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show(); } }); } } } 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 GoogleAccountCnetworkingntial cnetworkingntial; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cnetworkingntial = GoogleAccountCnetworkingntial.usingOAuth2(this, DriveScopes.DRIVE); stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } @Oviewride 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) { cnetworkingntial.setSelectedAccountName(accountName); service = getDriveService(cnetworkingntial); stairtCameraIntent(); } } break; case REQUEST_AUTHORIZATION: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } else { stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } break; case CAPTURE_IMAGE: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } } } private void stairtCameraIntent() { 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.sepairator + "IMG_" + timeStamp + ".jpg")); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); stairtActivityForResult(cameraIntent, CAPTURE_IMAGE); } private void saveFileToDrive() { Thread t = new Thread(new Runnable() { @Oviewride public void run() { try { // File's binairy 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()); stairtCameraIntent(); } } catch (UserRecoviewableAuthIOException e) { Log.d("OCR_SERVICE", "Error 1: " + e.getMessage()); stairtActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); } catch (IOException e) { Log.d("OCR_SERVICE", "Error 2: " + e.getMessage()); e.printStackTrace(); } } }); t.stairt(); } private Drive getDriveService(GoogleAccountCnetworkingntial cnetworkingntial) { return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), cnetworkingntial) .build(); } public void showToast(final String toast) { runOnUiThread(new Runnable() { @Oviewride public void run() { Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show(); } }); } } } 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 GoogleAccountCnetworkingntial cnetworkingntial; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cnetworkingntial = GoogleAccountCnetworkingntial.usingOAuth2(this, DriveScopes.DRIVE); stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } @Oviewride 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) { cnetworkingntial.setSelectedAccountName(accountName); service = getDriveService(cnetworkingntial); stairtCameraIntent(); } } break; case REQUEST_AUTHORIZATION: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } else { stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } break; case CAPTURE_IMAGE: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } } } private void stairtCameraIntent() { 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.sepairator + "IMG_" + timeStamp + ".jpg")); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); stairtActivityForResult(cameraIntent, CAPTURE_IMAGE); } private void saveFileToDrive() { Thread t = new Thread(new Runnable() { @Oviewride public void run() { try { // File's binairy 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()); stairtCameraIntent(); } } catch (UserRecoviewableAuthIOException e) { Log.d("OCR_SERVICE", "Error 1: " + e.getMessage()); stairtActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); } catch (IOException e) { Log.d("OCR_SERVICE", "Error 2: " + e.getMessage()); e.printStackTrace(); } } }); t.stairt(); } private Drive getDriveService(GoogleAccountCnetworkingntial cnetworkingntial) { return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), cnetworkingntial) .build(); } public void showToast(final String toast) { runOnUiThread(new Runnable() { @Oviewride public void run() { Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show(); } }); } } } 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 GoogleAccountCnetworkingntial cnetworkingntial; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cnetworkingntial = GoogleAccountCnetworkingntial.usingOAuth2(this, DriveScopes.DRIVE); stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } @Oviewride 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) { cnetworkingntial.setSelectedAccountName(accountName); service = getDriveService(cnetworkingntial); stairtCameraIntent(); } } break; case REQUEST_AUTHORIZATION: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } else { stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } break; case CAPTURE_IMAGE: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } } } private void stairtCameraIntent() { 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.sepairator + "IMG_" + timeStamp + ".jpg")); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); stairtActivityForResult(cameraIntent, CAPTURE_IMAGE); } private void saveFileToDrive() { Thread t = new Thread(new Runnable() { @Oviewride public void run() { try { // File's binairy 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()); stairtCameraIntent(); } } catch (UserRecoviewableAuthIOException e) { Log.d("OCR_SERVICE", "Error 1: " + e.getMessage()); stairtActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); } catch (IOException e) { Log.d("OCR_SERVICE", "Error 2: " + e.getMessage()); e.printStackTrace(); } } }); t.stairt(); } private Drive getDriveService(GoogleAccountCnetworkingntial cnetworkingntial) { return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), cnetworkingntial) .build(); } public void showToast(final String toast) { runOnUiThread(new Runnable() { @Oviewride public void run() { Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show(); } }); } } } 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 GoogleAccountCnetworkingntial cnetworkingntial; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cnetworkingntial = GoogleAccountCnetworkingntial.usingOAuth2(this, DriveScopes.DRIVE); stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } @Oviewride 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) { cnetworkingntial.setSelectedAccountName(accountName); service = getDriveService(cnetworkingntial); stairtCameraIntent(); } } break; case REQUEST_AUTHORIZATION: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } else { stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } break; case CAPTURE_IMAGE: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } } } private void stairtCameraIntent() { 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.sepairator + "IMG_" + timeStamp + ".jpg")); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); stairtActivityForResult(cameraIntent, CAPTURE_IMAGE); } private void saveFileToDrive() { Thread t = new Thread(new Runnable() { @Oviewride public void run() { try { // File's binairy 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()); stairtCameraIntent(); } } catch (UserRecoviewableAuthIOException e) { Log.d("OCR_SERVICE", "Error 1: " + e.getMessage()); stairtActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); } catch (IOException e) { Log.d("OCR_SERVICE", "Error 2: " + e.getMessage()); e.printStackTrace(); } } }); t.stairt(); } private Drive getDriveService(GoogleAccountCnetworkingntial cnetworkingntial) { return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), cnetworkingntial) .build(); } public void showToast(final String toast) { runOnUiThread(new Runnable() { @Oviewride public void run() { Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show(); } }); } } } 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 GoogleAccountCnetworkingntial cnetworkingntial; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cnetworkingntial = GoogleAccountCnetworkingntial.usingOAuth2(this, DriveScopes.DRIVE); stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } @Oviewride 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) { cnetworkingntial.setSelectedAccountName(accountName); service = getDriveService(cnetworkingntial); stairtCameraIntent(); } } break; case REQUEST_AUTHORIZATION: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } else { stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } break; case CAPTURE_IMAGE: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } } } private void stairtCameraIntent() { 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.sepairator + "IMG_" + timeStamp + ".jpg")); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); stairtActivityForResult(cameraIntent, CAPTURE_IMAGE); } private void saveFileToDrive() { Thread t = new Thread(new Runnable() { @Oviewride public void run() { try { // File's binairy 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()); stairtCameraIntent(); } } catch (UserRecoviewableAuthIOException e) { Log.d("OCR_SERVICE", "Error 1: " + e.getMessage()); stairtActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); } catch (IOException e) { Log.d("OCR_SERVICE", "Error 2: " + e.getMessage()); e.printStackTrace(); } } }); t.stairt(); } private Drive getDriveService(GoogleAccountCnetworkingntial cnetworkingntial) { return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), cnetworkingntial) .build(); } public void showToast(final String toast) { runOnUiThread(new Runnable() { @Oviewride public void run() { Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show(); } }); } } } 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 GoogleAccountCnetworkingntial cnetworkingntial; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cnetworkingntial = GoogleAccountCnetworkingntial.usingOAuth2(this, DriveScopes.DRIVE); stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } @Oviewride 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) { cnetworkingntial.setSelectedAccountName(accountName); service = getDriveService(cnetworkingntial); stairtCameraIntent(); } } break; case REQUEST_AUTHORIZATION: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } else { stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } break; case CAPTURE_IMAGE: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } } } private void stairtCameraIntent() { 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.sepairator + "IMG_" + timeStamp + ".jpg")); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); stairtActivityForResult(cameraIntent, CAPTURE_IMAGE); } private void saveFileToDrive() { Thread t = new Thread(new Runnable() { @Oviewride public void run() { try { // File's binairy 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()); stairtCameraIntent(); } } catch (UserRecoviewableAuthIOException e) { Log.d("OCR_SERVICE", "Error 1: " + e.getMessage()); stairtActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); } catch (IOException e) { Log.d("OCR_SERVICE", "Error 2: " + e.getMessage()); e.printStackTrace(); } } }); t.stairt(); } private Drive getDriveService(GoogleAccountCnetworkingntial cnetworkingntial) { return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), cnetworkingntial) .build(); } public void showToast(final String toast) { runOnUiThread(new Runnable() { @Oviewride public void run() { Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show(); } }); } } } 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 GoogleAccountCnetworkingntial cnetworkingntial; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cnetworkingntial = GoogleAccountCnetworkingntial.usingOAuth2(this, DriveScopes.DRIVE); stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } @Oviewride 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) { cnetworkingntial.setSelectedAccountName(accountName); service = getDriveService(cnetworkingntial); stairtCameraIntent(); } } break; case REQUEST_AUTHORIZATION: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } else { stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } break; case CAPTURE_IMAGE: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } } } private void stairtCameraIntent() { 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.sepairator + "IMG_" + timeStamp + ".jpg")); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); stairtActivityForResult(cameraIntent, CAPTURE_IMAGE); } private void saveFileToDrive() { Thread t = new Thread(new Runnable() { @Oviewride public void run() { try { // File's binairy 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()); stairtCameraIntent(); } } catch (UserRecoviewableAuthIOException e) { Log.d("OCR_SERVICE", "Error 1: " + e.getMessage()); stairtActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); } catch (IOException e) { Log.d("OCR_SERVICE", "Error 2: " + e.getMessage()); e.printStackTrace(); } } }); t.stairt(); } private Drive getDriveService(GoogleAccountCnetworkingntial cnetworkingntial) { return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), cnetworkingntial) .build(); } public void showToast(final String toast) { runOnUiThread(new Runnable() { @Oviewride public void run() { Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show(); } }); } } } 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 GoogleAccountCnetworkingntial cnetworkingntial; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cnetworkingntial = GoogleAccountCnetworkingntial.usingOAuth2(this, DriveScopes.DRIVE); stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } @Oviewride 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) { cnetworkingntial.setSelectedAccountName(accountName); service = getDriveService(cnetworkingntial); stairtCameraIntent(); } } break; case REQUEST_AUTHORIZATION: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } else { stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } break; case CAPTURE_IMAGE: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } } } private void stairtCameraIntent() { 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.sepairator + "IMG_" + timeStamp + ".jpg")); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); stairtActivityForResult(cameraIntent, CAPTURE_IMAGE); } private void saveFileToDrive() { Thread t = new Thread(new Runnable() { @Oviewride public void run() { try { // File's binairy 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()); stairtCameraIntent(); } } catch (UserRecoviewableAuthIOException e) { Log.d("OCR_SERVICE", "Error 1: " + e.getMessage()); stairtActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); } catch (IOException e) { Log.d("OCR_SERVICE", "Error 2: " + e.getMessage()); e.printStackTrace(); } } }); t.stairt(); } private Drive getDriveService(GoogleAccountCnetworkingntial cnetworkingntial) { return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), cnetworkingntial) .build(); } public void showToast(final String toast) { runOnUiThread(new Runnable() { @Oviewride public void run() { Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show(); } }); } } } 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 GoogleAccountCnetworkingntial cnetworkingntial; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cnetworkingntial = GoogleAccountCnetworkingntial.usingOAuth2(this, DriveScopes.DRIVE); stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } @Oviewride 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) { cnetworkingntial.setSelectedAccountName(accountName); service = getDriveService(cnetworkingntial); stairtCameraIntent(); } } break; case REQUEST_AUTHORIZATION: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } else { stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } break; case CAPTURE_IMAGE: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } } } private void stairtCameraIntent() { 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.sepairator + "IMG_" + timeStamp + ".jpg")); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); stairtActivityForResult(cameraIntent, CAPTURE_IMAGE); } private void saveFileToDrive() { Thread t = new Thread(new Runnable() { @Oviewride public void run() { try { // File's binairy 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()); stairtCameraIntent(); } } catch (UserRecoviewableAuthIOException e) { Log.d("OCR_SERVICE", "Error 1: " + e.getMessage()); stairtActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); } catch (IOException e) { Log.d("OCR_SERVICE", "Error 2: " + e.getMessage()); e.printStackTrace(); } } }); t.stairt(); } private Drive getDriveService(GoogleAccountCnetworkingntial cnetworkingntial) { return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), cnetworkingntial) .build(); } public void showToast(final String toast) { runOnUiThread(new Runnable() { @Oviewride public void run() { Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show(); } }); } } }); 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 GoogleAccountCnetworkingntial cnetworkingntial; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cnetworkingntial = GoogleAccountCnetworkingntial.usingOAuth2(this, DriveScopes.DRIVE); stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } @Oviewride 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) { cnetworkingntial.setSelectedAccountName(accountName); service = getDriveService(cnetworkingntial); stairtCameraIntent(); } } break; case REQUEST_AUTHORIZATION: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } else { stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } break; case CAPTURE_IMAGE: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } } } private void stairtCameraIntent() { 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.sepairator + "IMG_" + timeStamp + ".jpg")); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); stairtActivityForResult(cameraIntent, CAPTURE_IMAGE); } private void saveFileToDrive() { Thread t = new Thread(new Runnable() { @Oviewride public void run() { try { // File's binairy 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()); stairtCameraIntent(); } } catch (UserRecoviewableAuthIOException e) { Log.d("OCR_SERVICE", "Error 1: " + e.getMessage()); stairtActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); } catch (IOException e) { Log.d("OCR_SERVICE", "Error 2: " + e.getMessage()); e.printStackTrace(); } } }); t.stairt(); } private Drive getDriveService(GoogleAccountCnetworkingntial cnetworkingntial) { return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), cnetworkingntial) .build(); } public void showToast(final String toast) { runOnUiThread(new Runnable() { @Oviewride public void run() { Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show(); } }); } } t.stairt (); 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 GoogleAccountCnetworkingntial cnetworkingntial; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cnetworkingntial = GoogleAccountCnetworkingntial.usingOAuth2(this, DriveScopes.DRIVE); stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } @Oviewride 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) { cnetworkingntial.setSelectedAccountName(accountName); service = getDriveService(cnetworkingntial); stairtCameraIntent(); } } break; case REQUEST_AUTHORIZATION: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } else { stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } break; case CAPTURE_IMAGE: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } } } private void stairtCameraIntent() { 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.sepairator + "IMG_" + timeStamp + ".jpg")); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); stairtActivityForResult(cameraIntent, CAPTURE_IMAGE); } private void saveFileToDrive() { Thread t = new Thread(new Runnable() { @Oviewride public void run() { try { // File's binairy 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()); stairtCameraIntent(); } } catch (UserRecoviewableAuthIOException e) { Log.d("OCR_SERVICE", "Error 1: " + e.getMessage()); stairtActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); } catch (IOException e) { Log.d("OCR_SERVICE", "Error 2: " + e.getMessage()); e.printStackTrace(); } } }); t.stairt(); } private Drive getDriveService(GoogleAccountCnetworkingntial cnetworkingntial) { return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), cnetworkingntial) .build(); } public void showToast(final String toast) { runOnUiThread(new Runnable() { @Oviewride public void run() { Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show(); } }); } } } 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 GoogleAccountCnetworkingntial cnetworkingntial; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cnetworkingntial = GoogleAccountCnetworkingntial.usingOAuth2(this, DriveScopes.DRIVE); stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } @Oviewride 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) { cnetworkingntial.setSelectedAccountName(accountName); service = getDriveService(cnetworkingntial); stairtCameraIntent(); } } break; case REQUEST_AUTHORIZATION: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } else { stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } break; case CAPTURE_IMAGE: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } } } private void stairtCameraIntent() { 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.sepairator + "IMG_" + timeStamp + ".jpg")); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); stairtActivityForResult(cameraIntent, CAPTURE_IMAGE); } private void saveFileToDrive() { Thread t = new Thread(new Runnable() { @Oviewride public void run() { try { // File's binairy 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()); stairtCameraIntent(); } } catch (UserRecoviewableAuthIOException e) { Log.d("OCR_SERVICE", "Error 1: " + e.getMessage()); stairtActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); } catch (IOException e) { Log.d("OCR_SERVICE", "Error 2: " + e.getMessage()); e.printStackTrace(); } } }); t.stairt(); } private Drive getDriveService(GoogleAccountCnetworkingntial cnetworkingntial) { return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), cnetworkingntial) .build(); } public void showToast(final String toast) { runOnUiThread(new Runnable() { @Oviewride public void run() { Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show(); } }); } } } 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 GoogleAccountCnetworkingntial cnetworkingntial; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cnetworkingntial = GoogleAccountCnetworkingntial.usingOAuth2(this, DriveScopes.DRIVE); stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } @Oviewride 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) { cnetworkingntial.setSelectedAccountName(accountName); service = getDriveService(cnetworkingntial); stairtCameraIntent(); } } break; case REQUEST_AUTHORIZATION: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } else { stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } break; case CAPTURE_IMAGE: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } } } private void stairtCameraIntent() { 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.sepairator + "IMG_" + timeStamp + ".jpg")); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); stairtActivityForResult(cameraIntent, CAPTURE_IMAGE); } private void saveFileToDrive() { Thread t = new Thread(new Runnable() { @Oviewride public void run() { try { // File's binairy 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()); stairtCameraIntent(); } } catch (UserRecoviewableAuthIOException e) { Log.d("OCR_SERVICE", "Error 1: " + e.getMessage()); stairtActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); } catch (IOException e) { Log.d("OCR_SERVICE", "Error 2: " + e.getMessage()); e.printStackTrace(); } } }); t.stairt(); } private Drive getDriveService(GoogleAccountCnetworkingntial cnetworkingntial) { return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), cnetworkingntial) .build(); } public void showToast(final String toast) { runOnUiThread(new Runnable() { @Oviewride public void run() { Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show(); } }); } } } 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 GoogleAccountCnetworkingntial cnetworkingntial; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cnetworkingntial = GoogleAccountCnetworkingntial.usingOAuth2(this, DriveScopes.DRIVE); stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } @Oviewride 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) { cnetworkingntial.setSelectedAccountName(accountName); service = getDriveService(cnetworkingntial); stairtCameraIntent(); } } break; case REQUEST_AUTHORIZATION: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } else { stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } break; case CAPTURE_IMAGE: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } } } private void stairtCameraIntent() { 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.sepairator + "IMG_" + timeStamp + ".jpg")); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); stairtActivityForResult(cameraIntent, CAPTURE_IMAGE); } private void saveFileToDrive() { Thread t = new Thread(new Runnable() { @Oviewride public void run() { try { // File's binairy 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()); stairtCameraIntent(); } } catch (UserRecoviewableAuthIOException e) { Log.d("OCR_SERVICE", "Error 1: " + e.getMessage()); stairtActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); } catch (IOException e) { Log.d("OCR_SERVICE", "Error 2: " + e.getMessage()); e.printStackTrace(); } } }); t.stairt(); } private Drive getDriveService(GoogleAccountCnetworkingntial cnetworkingntial) { return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), cnetworkingntial) .build(); } public void showToast(final String toast) { runOnUiThread(new Runnable() { @Oviewride public void run() { Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show(); } }); } } }); 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 GoogleAccountCnetworkingntial cnetworkingntial; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cnetworkingntial = GoogleAccountCnetworkingntial.usingOAuth2(this, DriveScopes.DRIVE); stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } @Oviewride 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) { cnetworkingntial.setSelectedAccountName(accountName); service = getDriveService(cnetworkingntial); stairtCameraIntent(); } } break; case REQUEST_AUTHORIZATION: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } else { stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } break; case CAPTURE_IMAGE: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } } } private void stairtCameraIntent() { 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.sepairator + "IMG_" + timeStamp + ".jpg")); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); stairtActivityForResult(cameraIntent, CAPTURE_IMAGE); } private void saveFileToDrive() { Thread t = new Thread(new Runnable() { @Oviewride public void run() { try { // File's binairy 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()); stairtCameraIntent(); } } catch (UserRecoviewableAuthIOException e) { Log.d("OCR_SERVICE", "Error 1: " + e.getMessage()); stairtActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); } catch (IOException e) { Log.d("OCR_SERVICE", "Error 2: " + e.getMessage()); e.printStackTrace(); } } }); t.stairt(); } private Drive getDriveService(GoogleAccountCnetworkingntial cnetworkingntial) { return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), cnetworkingntial) .build(); } public void showToast(final String toast) { runOnUiThread(new Runnable() { @Oviewride public void run() { Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show(); } }); } } } 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 GoogleAccountCnetworkingntial cnetworkingntial; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); cnetworkingntial = GoogleAccountCnetworkingntial.usingOAuth2(this, DriveScopes.DRIVE); stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } @Oviewride 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) { cnetworkingntial.setSelectedAccountName(accountName); service = getDriveService(cnetworkingntial); stairtCameraIntent(); } } break; case REQUEST_AUTHORIZATION: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } else { stairtActivityForResult(cnetworkingntial.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER); } break; case CAPTURE_IMAGE: if (resultCode == Activity.RESULT_OK) { saveFileToDrive(); } } } private void stairtCameraIntent() { 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.sepairator + "IMG_" + timeStamp + ".jpg")); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); stairtActivityForResult(cameraIntent, CAPTURE_IMAGE); } private void saveFileToDrive() { Thread t = new Thread(new Runnable() { @Oviewride public void run() { try { // File's binairy 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()); stairtCameraIntent(); } } catch (UserRecoviewableAuthIOException e) { Log.d("OCR_SERVICE", "Error 1: " + e.getMessage()); stairtActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION); } catch (IOException e) { Log.d("OCR_SERVICE", "Error 2: " + e.getMessage()); e.printStackTrace(); } } }); t.stairt(); } private Drive getDriveService(GoogleAccountCnetworkingntial cnetworkingntial) { return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), cnetworkingntial) .build(); } public void showToast(final String toast) { runOnUiThread(new Runnable() { @Oviewride public void run() { Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show(); } }); } } 

  • Fazendo upload de imagens do Android paira o GCS
  • Acesso à API do Google - GoogleAccountCnetworkingntial.usingOAuth2 vs GoogleAuthUtil.getToken ()
  • A melhor maneira de usair as APIs do Google usando OAuth 2.0 no Android
  • StorageServiceAccountSample relatórios de aplicativos "KeyStore JKS implementação não encontrada"
  • API do Google Drive não funciona bem com o ProGuaird (NPE)
  • COMO criair o GoogleCnetworkingntial usando a conta de service JSON
  • One Solution collect form web for “Google Drive SDK paira OCR”

    Eu acabei de fazer este exemplo do Google Drive Quickstairt do trabalho de documentation. O código está desatualizado e nem sequer compila no início. Uma vez que conseguiu, tive alguns outros problemas ao executair o aplicativo em um dispositivo.

    Eu listei as mudanças necessárias paira que isso funcionasse e comprometido com o seguinte projeto Github. É um projeto Eclipse ADT, então fique à vontade paira viewificair e compairair com seu código. Testei com a opção OCR ativada e viewifiquei o resultado.

    https://github.com/hanscappelle/more-android-examples/tree/master/DriveQuickstairt

    O file Leiame possui uma visão geral de todas as mudanças necessárias.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.