java - 使用驱动器 api 将文件或数据库备份到驱动器时出错
问题描述
我想将我的应用程序数据库备份到用户的驱动器空间,例如 whatsapp 和大多数应用程序
我用了这段代码
public void connectToDrive(boolean backup) {
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(clscontxt);
if (account == null){signIn();}
else {
toast("already signed");
mDriveClient = Drive.getDriveClient(clscontxt, account);
mDriveResourceClient = Drive.getDriveResourceClient(this, account);
startDriveBackup();
}
}
private void signIn() {
GoogleSignInClient googlesigninclient = buildgooglesigninclient();
startActivityForResult(googlesigninclient.getSignInIntent(), 1000);
}
private GoogleSignInClient buildgooglesigninclient(){
GoogleSignInOptions signinoptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(Drive.SCOPE_FILE)
.build();
return GoogleSignIn.getClient(clscontxt,signinoptions);
}
private void startDriveBackup(){
mDriveResourceClient
.createContents()
.continueWithTask(
task -> createFileIntentSender(task.getResult()))
.addOnFailureListener(
e -> Log.w(TAG, "Failed to create new contents.", e));
}
private Task<Void> createFileIntentSender(DriveContents drivecontents){
try {
File dbfile = new File(getDatabasePath(Apconsts.db).toString());
FileInputStream fis = new FileInputStream(dbfile);
OutputStream outputStream = drivecontents.getOutputStream();
outputStream = new FileOutputStream(String.valueOf(drivecontents.getOutputStream()));
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) { outputStream.write(buffer, 0, length); }
} catch (IOException e) { e.printStackTrace(); }
MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
.setTitle("database_backup.db")
.setMimeType("application/db")
.build();
CreateFileActivityOptions createfileactivityoptions = new CreateFileActivityOptions.Builder()
.setInitialMetadata(metadataChangeSet)
.setInitialDriveContents(drivecontents)
.build();
return mDriveClient
.newCreateFileActivityIntentSender(createfileactivityoptions)
.continueWith(
task -> {
startIntentSenderForResult(task.getResult(), 1001, null, 0, 0, 0);
return null;
});
}
经过更多搜索并尝试了很多代码后,它签名正常但不能连续上传并显示此错误
com.google.android.gms.common.api.ApiException: 17: 错误解决被用户取消,原始错误消息:> API_UNAVAILABLE: null
有没有人有同样的问题并且可以解决这个问题?
解决方案
推荐阅读
- tensorflow - 如何将带有 kernel_regularizer 的 TensorFlow Conv2D 转换为 PyTorch
- python - 导入一个json文件,但一次只使用一行信息
- powershell - Powershell 脚本 - 等待 RoboCopy 完成
- c++ - 迭代两个向量并删除特定位置
- git - Git:如何在不接触工作目录的情况下切换分支?
- saxon - 我在哪里可以下载 Saxon 验证示例?
- r - 使用特定颜色为使用 networkD3::forceNetwork() 创建的网络中的每个节点设置边界
- python-3.x - Pygame中的蛇没有打开按键
- java - 复制番石榴可选
- c++ - 如何在 Windows 上的 cmake 中添加 .lib 库?