问题描述
我想SQLite数据库从数据文件夹复制使用模拟器到SD卡,我用下面code,我发现here.
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>
问题
的问题是,currentDB.exists()返回FALSE 。
在currentDB.getAbsolutePath()返回,这是数据\\数据\\ PACKAGENAME \\ \\数据库myDB.db
The "currentDB.getAbsolutePath()" returns the path which is "data\data\PackageName\databases\myDB.db"
这是数据库的正确位置,因为我可以找到使用它的Eclipse >> DDMS透视图>>文件浏览器
This is the correct location of the database, because i can find it using Eclipse >> DDMS perspective >> File Explorer
有人可以帮我找到问题为什么currentDB.exists()返回FALSE?
Can somebody help me find the issue why the "currentDB.exists()" returns FALSE?
感谢您的宝贵时间和放大器;帮助。
Thanks for your valuable time & help.
推荐答案
好,大家好我只是删除了 .db的扩展名和放大器;它的工作原理吧!
请参阅更新的code以下:
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替换为 MYDB
这篇关于Android的 - 无法找到文件,虽然它在数据文件夹中存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!