java - 如何在 Android 中从/向 Dropbox 导出导入 SQLite 数据库?

标签 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) {
                }
            }
        }
    }

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

最佳答案

只需在浏览器上从他们的网站访问 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();
    }
}

关于java - 如何在 Android 中从/向 Dropbox 导出导入 SQLite 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11487813/

相关文章:

java.security.AccessControlException : access denied (java. lang.RuntimePermission accessClassInPackage.sun.security.pkcs11)

java - 如何为 json 字符串编写通用的 getObject() 方法?

android - getIntent() 不检索先前 Activity 的 Intent 并返回 null

c# - UWP WebView 等效于 shouldOverrideUrlLoading Android

java - 我看不到数据,也看不到我创建的 sqlite 表

java - Firebase 云功能( strip : AddPaymentSource)

java - 如何在单击按钮时从 Java 代码运行批处理文件

android - 寻找某种方法来监视错误并在Unity上崩溃

database - 将本地 SQLite 数据库与服务器 SQLite 数据库同步

.net - 使用 IE 64 位插件在 Windows 7 64 位中加载 system.data.sqlite.dll 时出错