What happens is that I want to create a folder in google drive my perfect code, the problem that when using it and I do not know if it is because it does not have a good Internet connection or it is the google services that have not yet linked the account with the Google Drive API.
The code is easy, request the acceptance of services and then create a folder.
The purpose I want is to find a way to avoid this error that causes a collapse in the application causing the famous error "The application to be stopped".
The folder creation code is:
MetadataChangeSet changeSet =
new MetadataChangeSet.Builder()
.setTitle(foldername)
.build();
//Opción 1: Directorio raíz
DriveFolder folder = Drive.DriveApi.getAppFolder(apiClient);
//Opción 2: Otra carpeta distinta al directorio raiz
//DriveFolder folder =
// DriveId.decodeFromString("DriveId:CAESABjKGSD6wKnM7lQoAQ==").asDriveFolder();
folder.createFolder(apiClient, changeSet).setResultCallback(
new ResultCallback<DriveFolder.DriveFolderResult>() {
@Override
public void onResult(DriveFolder.DriveFolderResult result) {
if (result.getStatus().isSuccess()) {
Log.i("LOGTAG", "Fichero creado con ID = " + result.getDriveFolder().getDriveId());
SharedPreferences prefs = getSharedPreferences("MisPreferencias",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("ID", result.getDriveFolder().getDriveId().toString());
editor.commit();
} else {
Log.e("LOGTAG", "Error al crear el fichero");
}
}
});
Y The registration code is:
GoogleApiClient apiClient;
apiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this,0, this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addScope(Drive.SCOPE_APPFOLDER)
.build();
Currently I try to solve this error by placing an onConnectionFailed that warns with a toast to the user and starts the registration system again. But nothing happens.
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Toast.makeText(this, "Error de conexion!", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Ocurrio un Error volver a registrase por favor", Toast.LENGTH_LONG).show();
Toast.makeText(this, "Verificar conexion a internet", Toast.LENGTH_LONG).show();
apiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this,0, this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addScope(Drive.SCOPE_APPFOLDER)
.build();
}
If you know how to solve this issue. I will be happy to listen to you. Thank you.