Implementing text extraction from images using Google Drive API on Android

I’m working on an Android app that needs to extract text from images. I want to avoid using the Tesseract library and instead use Google Drive’s OCR capabilities.

I followed the Google Drive quickstart guide and successfully uploaded an image to Google Docs. The upload works fine and I get a success message with the filename.

For retrieving the extracted text, I implemented this method:

public void extractTextFromFile(File document) {
    String textExportUrl = document.getExportLinks().get("text/plain");
    
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet getRequest = new HttpGet(textExportUrl);
    HttpResponse httpResponse;
    
    StringBuilder textBuilder = new StringBuilder();
    BufferedReader reader = null;
    try {
        httpResponse = httpClient.execute(getRequest);
        reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
        String line;
        while ((line = reader.readLine()) != null) {
            textBuilder.append(line);
        }
        Log.d("Extracted Text", textBuilder.toString());
        reader.close();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

However, instead of getting the actual text from my uploaded image, I’m receiving HTML content that looks like Google Docs welcome page HTML.

I also tried using DriveRequest for downloading but got ClassCastException when trying to cast HttpRequest to DriveRequest.

How can I properly retrieve the OCR text results from the image I uploaded to Google Drive? Any help would be appreciated.

This happens because Google’s OCR isn’t instant. When you upload an image and convert it to Docs format, Google needs time to process and extract the text.

I hit this same issue last year. Fixed it by adding a polling check - upload the image, wait a few seconds, then periodically check if OCR is done before trying to grab the text.

Also double-check you’re setting the right MIME type during upload. You need to explicitly convert to ‘application/vnd.google-apps.document’ - if you just upload as a regular image, Google won’t run OCR at all.

yup, sounds like you might be missing the auth token in headers. also, ensure that the OCR process is complete before you make the request. sometimes it can take time, so just hang tight and try again later.

You’re hitting an authentication issue with the export URL format. Google Drive automatically runs OCR when you upload an image and convert it to Docs format, but you need proper auth headers to grab the extracted text.

Ditch the raw HttpClient approach. Use the Drive API’s files().export() method instead - it handles auth automatically. Try driveService.files().export(fileId, "text/plain").execute() where driveService is your authenticated Drive service.

Those export links from getExportLinks() need OAuth tokens to work. If you’re getting HTML back, it’s probably bouncing you to a login page because your request doesn’t have the right auth headers. Double-check that your Drive service is properly authenticated before making the export call.