我已经构建了一个用于管理事务的应用程序,我目前正在添加Dropbox备份。我这样做是通过将数据库文件上传到dropbox(似乎正确显示)。然后我想再次下载文件并覆盖现有的数据库。当我这样做时,数据库将保存为文件ei。通过context.fileList();而不是context.databaseList();如何处理数据库文件以使它们在正确的位置?
以下是我认为相关的代码:
private static class Downloader extends AsyncTask<Integer, Integer, Boolean>{
Context context;
@Override
protected void onPreExecute(){
context = SpendoBase.getContext();
}
@Override
protected Boolean doInBackground(Integer... arg0) {
System.out.println("DoInBackground:");
try {
List<DropboxAPI.Entry> entries = mDBApi.metadata("/", -1, null, true, null).contents;
File file;
FileOutputStream os;
int count = 0;
for(DropboxAPI.Entry entry: entries){
count++;
System.out.println("Entry.path(): " + entry.path + " " + count + "/" + entries.size());
file = new File(entry.path);
System.out.println("1");
os = context.openFileOutput(file.getName(), Context.MODE_PRIVATE);
System.out.println("2");
DropboxFileInfo info = mDBApi.getFile(entry.path, null, os, null);
os.flush();
os.close();
System.out.println("3 " + info);
}
} catch (DropboxException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
private static class Uploader extends AsyncTask<Integer, Integer, Boolean>{
String[] databaseList;
Context context;
@Override
protected void onPreExecute(){
context = SpendoBase.getContext();
databaseList = context.databaseList();
}
@Override
protected Boolean doInBackground(Integer... params) {
for(String dbName: databaseList){
try {
File f = context.getDatabasePath(dbName);
FileInputStream fis = new FileInputStream(f.getPath());
mDBApi.putFileOverwrite("/" + dbName, fis, f.length(), null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DropboxException e) {
e.printStackTrace();
}
}
return null;
}
}
private static class MetaReader extends AsyncTask<Integer, Integer, List<String>>{
@Override
protected List<String> doInBackground(Integer... arg0) {
try {
List<String> result = new Vector<String>();
DropboxAPI.Entry existingEntry = mDBApi.metadata("/", -1, null, true, null);
List<DropboxAPI.Entry> temp = existingEntry.contents;
for(int i = 0; i < temp.size(); i++){
File f = new File(temp.get(i).path);
result.add(f.getName());
}
return result;
} catch (DropboxException e) {
System.out.println("Something went wrong: " + e);
}
return null;
}
@Override
protected void onPostExecute(List<String> result){
for(String str:result){
System.out.println(str);
}
}
}
答案 0 :(得分:0)
我没有做太多的Android开发,所以我可能会离开这里,但为什么你不能再在context.getDatabasePath(dbName)
中使用Downloader
并将文件写入该路径?
答案 1 :(得分:0)
我设法解决了这个问题。我的错误只是我把数据库保存在错误的地方。
更改:
file = new File(entry.path);
System.out.println("1");
os = context.openFileOutput(file.getName(), Context.MODE_PRIVATE);
为:
file = new File("/data/data/com.SverkerSbrg.SpendoFull/databases/" + entry.path);
System.out.println("1");
os = new FileOutputStream(file.getPath());
解决了问题
答案 2 :(得分:0)
您需要根据目标手机将文件保存在/data/data/com.SverkerSbrg.SpendoFull/databases/此位置。