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