无法将SQLite数据库从SpeciperFolder.ApplicationData导出到SD卡Xamarin表单

发布于 2025-02-13 21:02:16 字数 873 浏览 3 评论 0原文

我目前正在开发一个使用SQLite-NET数据库的应用程序。我正在尝试将数据库复制/导出到我的SD卡。运行代码时,我会得到 system.nullrefrenceException:'对象引用未设置为对象的实例。

我尝试了几种解决方案,但我总是得到同样的例外。这些问题发生在 system.io.file.writeallbytes(filecopyname,bytes); 请提供帮助。

 private void CopyDBButton_Clicked(object sender, EventArgs e)
        {
            var basePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            var finalPath = Path.Combine(basePath, "Mydatabase");
            CopyDatabase(finalPath);
        }

 public static void CopyDatabase(string databasePath)
        {

            var bytes = System.IO.File.ReadAllBytes(databasePath);
            var fileCopyName = string.Format("/sdcard/Database_{0:dd-MM-yyyy_HH-mm-ss-tt}.db", System.DateTime.Now);
            System.IO.File.WriteAllBytes(fileCopyName, bytes);
        }

I am currently developing an app that uses the sqlite-net database. I am trying to copy/export the database to my SD Card. When I run the code I get a System.NullRefrenceException: 'Object reference not set to an instance of an object.'

I have tried several solutions but I always get the same exception. The issues occurs at the System.IO.File.WriteAllBytes(fileCopyName, bytes); Please help.

 private void CopyDBButton_Clicked(object sender, EventArgs e)
        {
            var basePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            var finalPath = Path.Combine(basePath, "Mydatabase");
            CopyDatabase(finalPath);
        }

 public static void CopyDatabase(string databasePath)
        {

            var bytes = System.IO.File.ReadAllBytes(databasePath);
            var fileCopyName = string.Format("/sdcard/Database_{0:dd-MM-yyyy_HH-mm-ss-tt}.db", System.DateTime.Now);
            System.IO.File.WriteAllBytes(fileCopyName, bytes);
        }

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

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

发布评论

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

评论(2

不离久伴 2025-02-20 21:02:16

您是否在清单文件中添加了下面列出的两个权限?


    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

如果您已经确认正确添加了上述权限,请尝试使用代码将数据从应用程序存储导出到您的SD卡上:

    private void Button_Clicked(object sender, EventArgs e)
        {
            string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "TodoSQLite.db3");
            var bytes = File.ReadAllBytes(path);
            var fileCopyName = string.Format("/sdcard/Database_{0:dd-MM-yyyy_HH-mm-ss-tt}.db", DateTime.Now);
            File.WriteAllBytes(fileCopyName, bytes);
        }

Did you add the two permissions listed below in your manifest file?


    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

If you have confirmed that the above permissions are correctly being added, please try the code to export data from the app storage onto your SD Card:

    private void Button_Clicked(object sender, EventArgs e)
        {
            string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "TodoSQLite.db3");
            var bytes = File.ReadAllBytes(path);
            var fileCopyName = string.Format("/sdcard/Database_{0:dd-MM-yyyy_HH-mm-ss-tt}.db", DateTime.Now);
            File.WriteAllBytes(fileCopyName, bytes);
        }

岁月打碎记忆 2025-02-20 21:02:16

问题是路径地址。我通过首先检查目录以查看是否存在来修复它,然后将数据库复制到新/现有文件中的目录。我现在遇到的问题是,该文件保存到电话,而不是SD卡,但是我很高兴备份文件最终保存。
以下是代码来解决该问题:

    private void CopyDBButton_Clicked(object sender, EventArgs e)
    {
        //Used to find the database in the special folder
        string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Mydatabase");

        //Used to locate the SD Card path
        string path1 = Path.Combine("/sdcard/", Android.OS.Environment.DirectoryDownloads);

        //Used to save the Database to a byte array
        var bytes = File.ReadAllBytes(path);

        //Used to check if the directory exists
        if (!Directory.Exists(path1))
        {
            //Directory.CreateDirectory(filePathDir);
            Console.WriteLine("Doesnt Exist");
        }
        else
        {
            Console.WriteLine("Does Exist");

            //Used to create the name of the new Database backup file
            var fileCopyName = string.Format(path1 + "/Database_{0:dd-MM-yyyy_HH-mm-ss-tt}.db", DateTime.Now);

            //Write to the new database backup file
            File.WriteAllBytes(fileCopyName, bytes);
        }
    }

The issue was the path address. I fixed it by checking for the directory first to see if it exists, then I copy the database to the directory in a new/existing file. The issue I have now is that the file saves to phone but not the SD card, but I am just happy that the backup file is finally saving.
Below is the code is used to fix the issue:

    private void CopyDBButton_Clicked(object sender, EventArgs e)
    {
        //Used to find the database in the special folder
        string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Mydatabase");

        //Used to locate the SD Card path
        string path1 = Path.Combine("/sdcard/", Android.OS.Environment.DirectoryDownloads);

        //Used to save the Database to a byte array
        var bytes = File.ReadAllBytes(path);

        //Used to check if the directory exists
        if (!Directory.Exists(path1))
        {
            //Directory.CreateDirectory(filePathDir);
            Console.WriteLine("Doesnt Exist");
        }
        else
        {
            Console.WriteLine("Does Exist");

            //Used to create the name of the new Database backup file
            var fileCopyName = string.Format(path1 + "/Database_{0:dd-MM-yyyy_HH-mm-ss-tt}.db", DateTime.Now);

            //Write to the new database backup file
            File.WriteAllBytes(fileCopyName, bytes);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文