2012-07-14 53 views
2

我已经成功地将Dropbox与我的应用程序集成在一起,可以上传并可以下载文件,但是当我上传一个sqlite数据库文件然后下载它时,该文件不包含任何数据。如何将导入的sqllite数据库导出到android的Drop box中?

这里是我的代码,我如何上传:

public void upload() { 

     FileInputStream inputStream = null; 
     try { 
      File file = new File(Environment.getExternalStorageDirectory() 
        .toString() + "/tripmileagedatabase"); 
      inputStream = new FileInputStream(file); 
      Entry newEntry = mDBApi.putFile("/tripmileagedatabase", inputStream, 
        file.length(), null, null); 
      Log.i("DbExampleLog", "The uploaded file's rev is: " + newEntry.rev); 
     } catch (DropboxUnlinkedException e) { 
      // User has unlinked, ask them to link again here. 
      Log.e("DbExampleLog", "User has unlinked."); 
     } catch (DropboxException e) { 
      Log.e("DbExampleLog", "Something went wrong while uploading."); 
     } catch (FileNotFoundException e) { 
      Log.e("DbExampleLog", "File not found."); 
     } finally { 
      if (inputStream != null) { 
       try { 
        inputStream.close(); 
       } catch (IOException e) { 
       } 
      } 
     } 

    } 

和我的下载方法是:

public void download() { 

     FileOutputStream outputStream = null; 
     try { 
      File file = new File(Environment.getExternalStorageDirectory() 
        .toString() + "/tripmileagedatabase"); 
      outputStream = new FileOutputStream(file); 
      DropboxFileInfo info = mDBApi.getFile("/tripmileagedatabase", null, 
        outputStream, null); 
      Log.i("DbExampleLog", "The file's rev is: " 
        + info.getMetadata().rev); 
      // /path/to/new/file.txt now has stuff in it. 
     } catch (DropboxException e) { 
      Log.e("DbExampleLog", "Something went wrong while downloading."); 
     } catch (FileNotFoundException e) { 
      Log.e("DbExampleLog", "File not found."); 
     } finally { 
      if (outputStream != null) { 
       try { 
        outputStream.close(); 
       } catch (IOException e) { 
       } 
      } 
     } 

    } 

但是,当我把这个数据库在我的应用程序,然后它表明该数据库中的数据,我现在能做什么?

回答

1

只需从浏览器的网站上访问sqlite文件,并检查sqlitebrowser以确保数据在数据库中退出。

这是我用过的。可能需要稍微修改它以使用ur api。但它应该工作

private void saveStreamToFile(InputStream responseStream){ 
    try{ 
     byte[] buf = new byte[1024];int len; 
     FileOutputStream out = getActivity().openFileOutput(
       "SQLIITE_FILE_NAME", Context.MODE_PRIVATE); 
     while ((len = in.read(buf)) > 0) { 
      out.write(buf, 0, len); 
     } 
     out.close(); 
     in.close(); 
     responseStream.close(); 

     SQLiteDatabase appDb = SQLiteDatabase.openDatabase(this.getFilesDir() 
       .getAbsolutePath()+"/SQLITE_FILE_NAME.db", null 
       ,SQLiteDatabase.NO_LOCALIZED_COLLATORS 
       |SQLiteDatabase.OPEN_READONLY); 
    }catch(IOException e){ 
     e.printStackTrace(); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 
相关问题