Android - 尽管数据文件夹中存在该文件,但找不到该文件?
我正在尝试使用模拟器将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我刚刚删除了“.db”扩展名&现在可以了!
请参阅下面更新的代码:
myDB.db 被替换为“myDB”。
Well guys i just removed the ".db" extension & it works now!
See the updated code below:
myDB.db is replaced with "myDB".