Search code examples
androidgoogle-drive-android-apicreatefile

How to connect Google Drive on Android Studio for create a folder or file?


I want to connect to Google Drive API in Android studio and create a file and read that file but I can not connect even though I have an apiKey. If I am debugging, then the application closes after GoogleApiClient.connect. I have been researching various pages but some of them also take SHA1 fingerprints, is this necessary? What could I have done wrong?


Solution

  • Follow the setup from the Drive API Android Quickstart

    1. Install the Android SDK.
    2. Download and configure the Google Play services SDK, which includes the Google Drive Android API.
    3. Register your application. To do this, you need to register a project in the Google API Console and get a signing certificate for your app.
    4. Add the required settings to your application's manifest.
    5. Add Drive services to your application. The easiest way to begin is by creating a file.

    Creating files

    You can create files in two ways with the Drive Android API: using the CreateFileActivityBuilder class, or using the createFile() method of the DriveFolder interface. The end result of either approach is the same. However, for instances where the user is specifying the location or title of the file, CreateFileActivityBuilder simplifies the process by providing a premade user interface. To create files programmatically or allow users to specify other metadata in a custom UI activity, you must use the the createFile() method.

    Create a folder in the root folder

    To create a folder, call DriveFolder.createFolder for the root folder. Pass the metadata containing the title and other attributes to set the values for the folder.

    Create folder in Drive REST API

    In the Drive API, a folder is essentially a file — one identified by the special folder MIME type application/vnd.google-apps.folder. You can create a new folder by inserting a file with this MIME type and a folder title. Do not include an extension when setting a folder title.