是否可以将数据库文件复制到SD卡上?

发布于 2024-12-10 02:44:35 字数 133 浏览 0 评论 0原文

我的 Android 手机上有一个数据库,我需要将信息存储到 SD 卡上。

是否可以将数据库文件以可读状态保存到SD卡上?我无法找到有关如何执行此操作的任何信息。

一些将数据库文件复制到 SD 卡的源代码将是理想的选择。

I have a database on my Android phone, and I need to get the information onto an SD card.

Is it possible to save the database file onto the SD card in a readable state? I haven't been able to find any information on how to do this.

Some source code that copies the database file to an SD card would be ideal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

擦肩而过的背影 2024-12-17 02:44:35

数据库文件就像任何其他文件一样,如果您制作二进制文件副本,它将起作用。

Java没有内置的文件复制方法,因此您可以使用以下方法:

用 Java 复制文件的标准简洁方法?

只是不要忘记添加写入 SD 卡的清单权限:
写入 SD 卡的权限

The database file is just like any other file, if you make a binary file copy it will work.

Java has no built in file copy method, so you can use this:

Standard concise way to copy a file in Java?

Just don't forget to add your manifest permission to write to the SD card:
Permission to write to the SD card

双手揣兜 2024-12-17 02:44:35

这是我从其他几个用户那里得到的脚本。看起来你可以告诉 android 在哪里存储文件,但是当你使用 adb shell 进入手机时,你可能很难找到它!

这段代码(我将其映射到操作栏中的临时按钮进行调试)将打印类似以下内容的内容:“数据库保存到:/storage/emulated/0/DB-DEBUG/todotable.db”,但进入我的 shell我实际上在以下位置找到了我的数据库:“/storage/emulated/legacy/DB-DEBUG/”...不确定这是怎么回事,但现在我可以使用 sqlite 浏览器查看我的数据库!

//db will reside in: /storage/emulated/legacy/DB_DEBUG
private void copyDatabase(Context c, String DATABASE_NAME) {
    String databasePath = c.getDatabasePath(DATABASE_NAME).getPath();
    File f = new File(databasePath);
    OutputStream myOutput = null;
    InputStream myInput = null;
    Log.d("testing", " testing db path " + databasePath);
    Log.d("testing", " testing db exist " + f.exists());

    if (f.exists()) {
        try {
            File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DB-DEBUG");
            if (!directory.exists()){
                directory.mkdir();
            }

            String copyPath = directory.getAbsolutePath() + "/" + DATABASE_NAME;
            myOutput = new FileOutputStream(copyPath);
            myInput = new FileInputStream(databasePath);

            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }

            myOutput.flush();
            Toast.makeText(getBaseContext(), "Your database copied to: " + copyPath, Toast.LENGTH_LONG).show();
            Log.d("testing", " database saved to: " + copyPath);

        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();

        } finally {
            try {
                if (myOutput != null) {
                    myOutput.close();
                    myOutput = null;
                }
                if (myInput != null) {
                    myInput.close();
                    myInput = null;
                }
            } catch (Exception e) {
                Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    }
}

Here's a script I've bastardized from several other users on SO. It looks like you can tell android where to store the file, but when you go into the phone with adb shell you might have a hard time finding it!

This code (which I mapped to a temporary button in my action bar for debugging) would print something like: "database saved to: /storage/emulated/0/DB-DEBUG/todotable.db", but going into the shell on my phone I actually found my database at: "/storage/emulated/legacy/DB-DEBUG/"... not sure what's up with that, but now I can check out my database with an sqlite browser!

//db will reside in: /storage/emulated/legacy/DB_DEBUG
private void copyDatabase(Context c, String DATABASE_NAME) {
    String databasePath = c.getDatabasePath(DATABASE_NAME).getPath();
    File f = new File(databasePath);
    OutputStream myOutput = null;
    InputStream myInput = null;
    Log.d("testing", " testing db path " + databasePath);
    Log.d("testing", " testing db exist " + f.exists());

    if (f.exists()) {
        try {
            File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DB-DEBUG");
            if (!directory.exists()){
                directory.mkdir();
            }

            String copyPath = directory.getAbsolutePath() + "/" + DATABASE_NAME;
            myOutput = new FileOutputStream(copyPath);
            myInput = new FileInputStream(databasePath);

            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }

            myOutput.flush();
            Toast.makeText(getBaseContext(), "Your database copied to: " + copyPath, Toast.LENGTH_LONG).show();
            Log.d("testing", " database saved to: " + copyPath);

        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();

        } finally {
            try {
                if (myOutput != null) {
                    myOutput.close();
                    myOutput = null;
                }
                if (myInput != null) {
                    myInput.close();
                    myInput = null;
                }
            } catch (Exception e) {
                Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文