Reputation: 205
I'm trying to upload an image to google drive from android code and perform ocr on it. Can't figure out how the ocr option should be requested. I read several answers here on SO saying there is a .insert() method somewhere that I should be using but I can't find such a method. The Android Developer site fails to provide proper documentation on this api.
If someone could just tell me what to add to my code please:
public class MainActivity extends Activity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
...
private class EditFileAsyncTask extends AsyncTask<DriveApi.ContentsResult, Void, Boolean> {
@Override
protected Boolean doInBackground(DriveApi.ContentsResult... params) {
DriveApi.ContentsResult contentsResult = params[0];
if (!contentsResult.getStatus().isSuccess()) {
showMessage("Failed to create new contents.");
return false;
}
showMessage("New contents created.");
OutputStream outputStream = contentsResult.getContents().getOutputStream();
ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
mBitmapToSave.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream);
try {
outputStream.write(bitmapStream.toByteArray());
} catch (IOException e) {
showMessage("Unable to write file contents.");
e.printStackTrace();
}
MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
.setMimeType("image/jpeg")
.setTitle("rochale1.jpg")
.build();
IntentSender intentSender = Drive.DriveApi
.newCreateFileActivityBuilder()
.setInitialMetadata(metadataChangeSet)
.setInitialContents(contentsResult.getContents())
.build(mGoogleApiClient);
try {
startIntentSenderForResult(intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0);
} catch (SendIntentException e) {
showMessage("Failed to launch file chooser.");
e.printStackTrace();
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
if (!result) {
showMessage("Error while editing contents");
return;
}
showMessage("Successfully edited contents");
}
}
}
I am assuming this task would be the place to request ocr, if not please tell me.
Upvotes: 2
Views: 310
Reputation: 360
Here is a PHP script to upload the file using insert method which seems to be the task you are trying to accomplish for Android - not sure if this will help but this does explain the insert method below....
You can also edit the file properties after its loaded which might be a solution for you using "patch" method
https://developers.google.com/drive/v2/reference/files/patch
https://developers.google.com/drive/v2/reference/files/insert
function uploadFile($service,$title,$description,$filePath,$mtype){
//Insert a file
$file = new Google_Service_Drive_DriveFile();
$file->setTitle($title.".pdf");
$file->setDescription($description);
$file->setMimeType('application/pdf');
$data = file_get_contents($filePath);
sleep(1);
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => $mtype,
'uploadType' => 'multipart',
'convert' => true,
'ocr'=> true
));
return ($createdFile);
}
Upvotes: 1