Android - 尽管数据文件夹中存在该文件,但找不到该文件?

发布于 2024-12-15 10:12:54 字数 1190 浏览 3 评论 0原文

我正在尝试使用模拟器将 SQLite 数据库从数据文件夹复制到 SDCard,我使用下面的代码,我发现它 此处

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

    if (sd.canWrite()) {
        String currentDBPath = "\\data\\PackageName\\databases\\myDB.db";
        String backupDBPath = "myDB.db";
        File currentDB = new File(data, currentDBPath);
        File backupDB = new File(sd, backupDBPath);

        if (currentDB.exists()) 
        {
           // code to copy from currentDB  to backupDB 
        }
    }
} catch (Exception e) {
}

对于以下写入权限,也将添加到清单文件中:

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

问题

问题是“currentDB.exists()”返回FALSE

“currentDB.getAbsolutePath()”返回的路径是“data\data\PackageName\databases\myDB.db”

这是数据库的正确位置,因为我可以使用 Eclipse >> 找到它DDMS视角>>文件资源管理器

有人可以帮我找到为什么“currentDB.exists()”返回 FALSE 的问题吗?

感谢您抽出宝贵的时间&帮助。

I am trying to copy the SQLite database from the data folder to SDCard using Emulator, i am using below code, which i found here.

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

    if (sd.canWrite()) {
        String currentDBPath = "\\data\\PackageName\\databases\\myDB.db";
        String backupDBPath = "myDB.db";
        File currentDB = new File(data, currentDBPath);
        File backupDB = new File(sd, backupDBPath);

        if (currentDB.exists()) 
        {
           // code to copy from currentDB  to backupDB 
        }
    }
} catch (Exception e) {
}

For write permission below is also added to manifest file:

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

Issue:

The issue is that the "currentDB.exists()" returns FALSE.

The "currentDB.getAbsolutePath()" returns the path which is "data\data\PackageName\databases\myDB.db"

This is the correct location of the database, because i can find it using Eclipse >> DDMS perspective >> File Explorer

Can somebody help me find the issue why the "currentDB.exists()" returns FALSE?

Thanks for your valuable time & help.

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

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

发布评论

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

评论(1

表情可笑 2024-12-22 10:12:54

好吧,我刚刚删除了“.db”扩展名&现在可以了!
请参阅下面更新的代码:

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

    if (sd.canWrite()) {
        String currentDBPath = "\\data\\PackageName\\databases\\myDB";
        String backupDBPath = "myDB";
        File currentDB = new File(data, currentDBPath);
        File backupDB = new File(sd, backupDBPath);

        if (currentDB.exists()) 
        {
           // code to copy from currentDB  to backupDB 
        }
    }
} catch (Exception e) {
}

myDB.db 被替换为“myDB”。

Well guys i just removed the ".db" extension & it works now!
See the updated code below:

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

    if (sd.canWrite()) {
        String currentDBPath = "\\data\\PackageName\\databases\\myDB";
        String backupDBPath = "myDB";
        File currentDB = new File(data, currentDBPath);
        File backupDB = new File(sd, backupDBPath);

        if (currentDB.exists()) 
        {
           // code to copy from currentDB  to backupDB 
        }
    }
} catch (Exception e) {
}

myDB.db is replaced with "myDB".

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