Android 复制文件不适用于本地 SQLite 文件

发布于 2024-12-31 22:09:35 字数 1163 浏览 3 评论 0原文

我正在使用这段代码将文件从内部存储复制到外部存储,而不会丢失任何内容:

public static void copyFile(String currentDBPath, String backupDBPath) {
    try {
        File sd = Environment.getExternalStorageDirectory();
        //File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            File currentDB = new File(currentDBPath);
            File backupDB = new File(backupDBPath);

            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(currentDB).getChannel();
                FileChannel dst = new FileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

当我尝试从内部存储或 txt 文件复制图像时,它工作正常,没有任何错误,并且新文件位于 SD 卡中。但是,如果我尝试从 /data/data/package/databases/system.sqlite 复制本地数据库文件,它会在给定的目标中创建一个新文件,但新的数据库文件是空的,没有任何内容表格甚至数据。

知道为什么它只发生在数据库文件上吗?

编辑:

正如我注意到的,复制的新数据库文件与原始数据库文件一样大(145KB),但是当我用sqlite浏览器打开它时,没有表,没有数据...什么都没有。

I'm using this piece of code to copy files from internal to external storage without losing any :

public static void copyFile(String currentDBPath, String backupDBPath) {
    try {
        File sd = Environment.getExternalStorageDirectory();
        //File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            File currentDB = new File(currentDBPath);
            File backupDB = new File(backupDBPath);

            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(currentDB).getChannel();
                FileChannel dst = new FileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

When I try to copy images from internal storage or txt files it's working fine, without any errors and the new files are in sd card. But if I try to copy my local database files from /data/data/package/databases/system.sqlite it's creating a new file in the given destination, but the new database file is empty, without any tables or even data on it.

Any ideas why it's happening only with database files?

Edit:

As I noticed, the new database file which is copied is as big as the original one (145KB), but when I open it with sqlite browser there are no tables, no data...nothing.

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

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

发布评论

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

评论(1

用心笑 2025-01-07 22:09:35

这就是我们使用的:

            try {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();

            if (sd.canWrite()) {
                String currentDBPath = "/data/com.our.package/databases/main.db";
                String backupDBPath = "main_" + additionalInfo + ".db";
                File currentDB = new File(data, currentDBPath);
                File backupDB = new File(sd, backupDBPath);

                if (currentDB.exists()) {
                    FileChannel src = new FileInputStream(currentDB).getChannel();
                    FileChannel dst = new FileOutputStream(backupDB).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                } else {
                    Log.e(TAG, "File does not exist: " + currentDBPath);
                }
            } else {
                Log.e(TAG, "SDCard not writable, backup aborted.");
            }
        } catch (Exception ex) {
            Log.e(TAG, "Error backing up database to sdcard.", ex);
            return getResources().getInteger(R.integer.unhandled_exception_from_api_service);
        }

我们能够将数据库备份到 SD 卡并使用 SQLite 浏览器将其读回。

This is what we use:

            try {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();

            if (sd.canWrite()) {
                String currentDBPath = "/data/com.our.package/databases/main.db";
                String backupDBPath = "main_" + additionalInfo + ".db";
                File currentDB = new File(data, currentDBPath);
                File backupDB = new File(sd, backupDBPath);

                if (currentDB.exists()) {
                    FileChannel src = new FileInputStream(currentDB).getChannel();
                    FileChannel dst = new FileOutputStream(backupDB).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                } else {
                    Log.e(TAG, "File does not exist: " + currentDBPath);
                }
            } else {
                Log.e(TAG, "SDCard not writable, backup aborted.");
            }
        } catch (Exception ex) {
            Log.e(TAG, "Error backing up database to sdcard.", ex);
            return getResources().getInteger(R.integer.unhandled_exception_from_api_service);
        }

We're able to back up the database to the sdcard and read it back using SQLite Browser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文