我需要创建我的应用程序的备份,其中包括创建2个数据库的备份以及使用Google驱动器API的共享首选项。我能得到的应用程序进行认证,并使用下面的代码创建驱动器的新文件夹:Google驱动器来备份和恢复Android应用程序的数据库和共享首选项
public class MainActivity2 extends BaseDemoActivity {
DriveId folderId;
@Override
public void onConnected(Bundle connectionHint) {
super.onConnected(connectionHint);
MetadataChangeSet changeSet = new MetadataChangeSet.Builder().setTitle("New folder").build();
Drive.DriveApi.getRootFolder(getGoogleApiClient())
.createFolder(getGoogleApiClient(), changeSet)
.setResultCallback(folderCreatedCallback);
}
ResultCallback<DriveFolderResult> folderCreatedCallback = new ResultCallback<DriveFolderResult>() {
@Override
public void onResult(DriveFolderResult result) {
if (!result.getStatus().isSuccess()) {
showMessage("Error while trying to create the folder");
return;
}
folderId = result.getDriveFolder().getDriveId();
showMessage("Created a folder: " + result.getDriveFolder().getDriveId());
}
};
}
我插入使用
fileUri = Uri.fromFile(new java.io.File(Environment.getDataDirectory().getPath()
+ "/data/com.example.myapp/databases/mydb.db"));
java.io.File fileContent = new java.io.File(fileUri.getPath());
FileContent mediaContent = new FileContent(getMimeType("db"), fileContent);
File body = new com.google.api.services.drive.model.File();
body.setTitle(fileContent.getName());
body.setMimeType(getMimeType("db"));
File file = service.files().insert(body, mediaContent).execute();
能正常工作的文件。
我恢复使用
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
try {
fileId="0B7Gol85MIbTJLTRxX1hZdDJjaEE";
credential.setSelectedAccountName("the same drive account");
service = getDriveService(credential);
if (fileId != null) {
File file = service.files().get(fileId).execute();
downloadFile(service, file);
}
else return null;
} catch (Throwable tr) {
}
return fileId;
}
private void downloadFile(Drive service, File file) {
InputStream mInput=null;
FileOutputStream mOutput=null;
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
try {
HttpResponse resp = service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl())).execute();
mInput = resp.getContent();
String outFileName = "file://"+Environment.getDataDirectory().getPath() + "/data/com.example.myapp/databases/InvoiceDataBase.db";
Log.e("com.example.myapp", "getDatabasePath="+ getDatabasePath(""));
Log.e("com.example.myapp", "outFileName="+outFileName);
// String outFileName = "../databases/" + "Quickpay.db";
mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer)) > 0) {
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
} catch (IOException e) {
// An error occurred.
e.printStackTrace();
// return null;
}
finally {
try {
//Close the streams
if(mOutput != null){
mOutput.close();
}
if(mInput != null){
mInput.close();
}
} catch (IOException e) {
Log.e("com.example.myapp", "failed to close databases");
}
}
} else {
// The file doesn't have any content stored on Drive.
// return null;
Log.e("com.example.myapp", "No content on Drive");
}
}
当我以后再备份恢复,我得到
“/data/data/com.example.myapp/databases/mydb:打开失败:ENOENT(没有这样的文件或目录)“
现在我需要上传我的”.db“文件在这个文件夹中,并上传共享首选项作为XML文件。所以我的问题是:
- 如何将我的应用程序数据库上传到此文件夹? 解决了。
- 如何将数据库文件恢复到应用程序?
- 登录到他的Google云端硬盘帐户后,用户是否可以完全访问这些文件?有没有其他的方法来备份和恢复?我无法使用“Android备份服务”,因为这不能成为强制备份。
TIA。
你能帮我吗? – Lunchbox
对不起,你还在寻找这个解决方案?我没有在网上SO –
没问题,是的,我是。我试图使用下拉框来实现相同的目标,但我也很困难 – Lunchbox