2013-10-16 91 views
0

我已经构建了一个应用程序来管理事务,我目前正在添加Dropbox备份。我通过将数据库文件上传到Dropbox(这似乎正确显示)来实现这一点。然后我想再次下载这些文件并覆盖现有的数据库。当我这样做时,数据库被保存为文件ei。通过context.fileList()获取列表。而不是context.databaseList();我如何处理数据库文件以将它们放在正确的位置?Sqlite数据库保存为文件

这里是我认为的代码相关:

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

我没有做很多Android开发,所以我可能是大错特错这里,但为什么你就不能再次在Downloader使用context.getDatabasePath(dbName)并将该文件写入该路径?

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()); 

问题解决了

0

你需要以root你的目标手机,以节省您的/data/data/com.SverkerSbrg文件。 SpendoFull /数据库/这个位置。

+1

您可以直接在内部或外部存储上存储任何文件。请从http://developer.android.com/training/basics/data-storage/files.html获取帮助以存储文件。 –