如何从Android中导入SQLite数据库到Dropbox?

时间:2012-07-14 22:20:48

标签: java android sqlite dropbox

我已成功将Dropbox与我的应用程序集成,并且可以上传并可以下载文件,但是当我上传sqlite数据库文件然后下载后,该文件不包含任何数据。

以下是我上传的代码:

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 个答案:

答案 0 :(得分:1)

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

这就是我用过的东西。可能需要稍微修改它以使用你的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();
    }
}