3

I followed this documentation in order to upload files to Google Drive: https://developers.google.com/drive/android/files
Now every time I want to upload a file a popup of Google Drive appears, asking me where to upload it and with which name. I don't want that popup, I want to upload directly the file.
Is there any method in order to do that? I can't find anything into the docs. I found that too: https://developers.google.com/drive/api/v3/integrate-create, but I don't know if I can use it. Can you give me some hints?

EDIT:
This is my build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
        mavenCentral()
        maven { url 'https://maven.google.com' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
        maven { url 'https://maven.google.com' }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
Yes92
  • 301
  • 4
  • 15
  • I do that autommatically in my project, but I'm not using Google Drive for Android (`com.google.android.gms:play-services-drive:15.0.1`), I'm using `com.google.apis:google-api-services-drive` instead. Are you using `com.google.android.gms:play-services-drive:15.0.1` aren't you? – Marc Estrada May 24 '18 at 16:32
  • Yes I am! I will try your solution. – Yes92 May 24 '18 at 17:58
  • Let me know if you want a code example (as an answer) of how I do it. It's not easy, but works. – Marc Estrada May 24 '18 at 18:06
  • In the answer you have the code, hope this help you – Marc Estrada May 24 '18 at 21:09

1 Answers1

5

It can be achieved using the Google Drive REST API for Android (it uses the Google Drive API directly), instead of using Google Drive SDK for Android.

First of all, you need to login in Google Drive by choosing a Google Account of the device. I guess you've done it because in Google Drive SDK also needs to login. To login I use this dependence.

compile 'com.google.android.gms:play-services-auth:10.2.0'

Then, to upload a file (by creating it in the REST API) do the following.

First, import the Google Drive REST API dependencies for Android:

compile('com.google.api-client:google-api-client-android:1.22.0') {
    exclude group: 'org.apache.httpcomponents'
}
compile('com.google.apis:google-api-services-drive:v3-rev75-1.22.0') {
    exclude group: 'org.apache.httpcomponents'
}

Second, create the service object in order to connect with the API:

private Drive createService(Context context) {
   GoogleAccountCredential mCredential = GoogleAccountCredential.usingOAuth2(context.getApplicationContext(), Arrays.asList(new String[]{DriveScopes.DRIVE})).setBackOff(new ExponentialBackOff());
   mCredential.setSelectedAccountName("your_logged_account_name");

   HttpTransport transport = AndroidHttp.newCompatibleTransport();
   JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
   mService = new com.google.api.services.drive.Drive.Builder(
        transport, jsonFactory, mCredential)
        .setApplicationName(context.getString(R.string.app_name))
        .build();

  return mService;
}

Afterwards, when you are logged in and you have the Drive instance (service), you can create a file:

public void uploadFile(java.io.File fileContent) {
   try {
     com.google.api.services.drive.model.File file = new com.google.api.services.drive.model.File();
     file.setName("filen_ame");

     List<String> parents = new ArrayList<>(1);
     parents.add("parent_folder_id"); // Here you need to get the parent folder id
     file.setParents(parents);

     FileContent mediaContent = new FileContent("your_file_mime_type", fileContent);
     mService.files().create(file, mediaContent).setFields("id").execute();
     Log.d(TAG, "File uploaded");
   } catch (IOException e) {
     Log.e(TAG, "Error uploading file: ", e);
     e.printStackTrace();
   }
}

Please note I used the full class name in order to differentiate the Java File to the Drive File.

Also note that this call is synchronous, so you must call it in a non-UiThread.

This method is able to create and upload a file to Google Drive without showing any chooser dialog and without user interaction.

Here you have some documentation.

Hope this helps.

Marc Estrada
  • 1,657
  • 10
  • 20
  • thank you! Now I'm getting this error : `Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve com.google.api-client:google-api-client-android:1.23.0`. Do you know how to solve it? – Yes92 May 25 '18 at 12:42
  • I tried yours dependencies and even the ones in the docs... the problem persist – Yes92 May 25 '18 at 12:51
  • Add the Google maven repository in your Gradle project file. `maven { url 'https://maven.google.com' }` inside allprojects and inside repositories – Marc Estrada May 25 '18 at 13:14
  • I edited the question because the problem persisti. I posted my build.gradle. Any suggestion? – Yes92 May 25 '18 at 18:22
  • 1
    Great! Btw, if you has `google()` in your repositories, removing `maven { url 'https://maven.google.com' }` also should work because `google()` is a shortcut of the Google maven url repository. – Marc Estrada May 25 '18 at 18:55
  • Ok! Thank yout! – Yes92 May 25 '18 at 20:55