Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading text from Google Drive files

Is there any possible way to read the text from a file on Google Drive and store it in a String? This file may contain images as well. I was looking into the Google Drive SDK but they only allow us to download the entire file. How should I go about doing this?

like image 310
Nick Richards Avatar asked Dec 05 '25 05:12

Nick Richards


1 Answers

From Files.get() documentation.

private static InputStream downloadFile(Drive service, File file) {
  if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
    try {
      HttpResponse resp =
          service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl()))
              .execute();
      return resp.getContent();
    } catch (IOException e) {
      // An error occurred.
      e.printStackTrace();
      return null;
    }
  } else {
    // The file doesn't have any content stored on Drive.
    return null;
  }
}

You can convert InputStream to String or File as you want.

like image 180
JunYoung Gwak Avatar answered Dec 07 '25 20:12

JunYoung Gwak