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
}
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.