2

我已经成功地将 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) {
                }
            }
        }
    }

但是当我在我的应用程序中使用这个数据库时,它在那个数据库中没有显示任何数据,我现在该怎么办?

4

1 回答 1

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();
    }
}
于 2012-07-15T01:49:41.813 回答