Bom dia!!
Material do Google não sei te dizer, mas já fiz algo parecido… Para exportar o DB fiz dessa forma:
@SuppressWarnings("resource")
private void exportDatabase(Context ctx) {
File backupDB = null;
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//" + ctx.getPackageName()
+ "//databases//" + "NomeDoBanco.db";
File currentDB = new File(data, currentDBPath);
backupDB = new File(sd, "NomeDoBanco.db");
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
} else {
System.out.println("Não pode escrever no sd");
}
} catch (Exception e) {
System.out.println("Exception:" + e);
}
try {
//Intent para exportar
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{});
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Banco de dados");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(backupDB));
ctx.startActivity(Intent.createChooser(intent, "Exportar banco de dados"));
}catch(Exception e){
System.out.println("Exception:"+e);
}
}
Essa intent chama várias coisas, inclusive o google drive. Para chamar somente ele, vai ter que mudar essa chamada. Para importar o DB, vc pode fazer dessa forma (Só teria que alterar para pegar do google drive. Aqui pego o banco do diretório de downloads):
public void importa(){
try {
String from = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/NomeDoBanco.db";
String outFileName = Environment.getDataDirectory() + "//data//" + getContext().getPackageName() + "//databases//NomeDoBanco.db";
File temp = new File(outFileName);
if (temp.exists()) {
temp.delete();
}
temp.createNewFile();
String to = Environment.getDataDirectory() + "//data//" + getContext().getPackageName() + "//databases//";
ImportarBAnco(from, to);
} catch (Exception e) {
System.out.println("Exception:"+e.toString());
}
}
private void ImportarBAnco(String from, String to) {
try {
File sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
int end = from.toString().lastIndexOf("/");
String str1 = from.toString().substring(0, end);
String str2 = from.toString().substring(end+1, from.length());
File source = new File(str1, str2);
File destination= new File(to, str2);
if (source.exists()) {
FileChannel src = new FileInputStream(source).getChannel();
FileChannel dst = new FileOutputStream(destination).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
System.out.println("Importado com sucesso");
}else{
System.out.println("Source não existe");
}
}else{
System.out.println("Não tem permissão");
}
} catch (Exception e) {
System.out.println("Erro ao importar:"+e.toString());
}
}
Esse código ainda não está funcionando no Android Oreo, tenho que ver o motivo, mas acho que é algum tipo de permissão.