Java 是否可以将数据库文件保存到SD卡?
可能重复:Java 是否可以将数据库文件保存到SD卡?,java,android,database,save,sd-card,Java,Android,Database,Save,Sd Card,可能重复: 我的Android手机上有一个数据库,我需要把信息输入SD卡 是否可以将数据库文件以可读状态保存到SD卡上?我还没有找到任何关于如何做到这一点的信息。我知道数据库的名称和字段等 我找到了一些例子,展示了如何保存到SD卡,但并不完全是我需要的 一些将数据库文件复制到SD卡的源代码将是完美的 希望这个问题足够清楚 是的。以下是我使用的函数: public void copyDBToSDCard() { try { InputStream myInput = ne
我的Android手机上有一个数据库,我需要把信息输入SD卡 是否可以将数据库文件以可读状态保存到SD卡上?我还没有找到任何关于如何做到这一点的信息。我知道数据库的名称和字段等 我找到了一些例子,展示了如何保存到SD卡,但并不完全是我需要的 一些将数据库文件复制到SD卡的源代码将是完美的
希望这个问题足够清楚 是的。以下是我使用的函数:
public void copyDBToSDCard() {
try {
InputStream myInput = new FileInputStream("/data/data/com.myproject/databases/"+DATABASE_NAME);
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/"+DATABASE_NAME);
if (!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
Log.i("FO","File creation failed for " + file);
}
}
OutputStream myOutput = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/"+DATABASE_NAME);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
Log.i("FO","copied");
} catch (Exception e) {
Log.i("FO","exception="+e);
}
}
对于我参与的一个项目,我在主屏幕中放置了一个菜单选项,我可以随时调用该功能。然后,我会将数据库移动到我的桌面,并使用用于FireFox的SQLite Manager插件打开它。是的。以下是我使用的函数:
public void copyDBToSDCard() {
try {
InputStream myInput = new FileInputStream("/data/data/com.myproject/databases/"+DATABASE_NAME);
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/"+DATABASE_NAME);
if (!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
Log.i("FO","File creation failed for " + file);
}
}
OutputStream myOutput = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/"+DATABASE_NAME);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
Log.i("FO","copied");
} catch (Exception e) {
Log.i("FO","exception="+e);
}
}
对于我参与的一个项目,我在主屏幕中放置了一个菜单选项,我可以随时调用该功能。然后,我会将数据库移动到我的桌面,并使用用于FireFox的SQLite管理器插件打开它。当然。如果这是应用程序中存在的数据库,则可以通过
Context.getDatabasePath()
获取对db文件的引用,并将数据库名称传递给它。从这里开始,它只是一个例行的文件复制操作:
//Get a reference to the database
File dbFile = mContext.getDatabasePath("mydb");
//Get a reference to the directory location for the backup
File exportDir = new File(Environment.getExternalStorageDirectory(), "myAppBackups");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File backup = new File(exportDir, dbFile.getName());
//Check the required operation String command = params[0];
//Attempt file copy
try {
backup.createNewFile();
fileCopy(dbFile, backup);
} catch (IOException e) {
/*Handle File Error*/
}
其中方法fileCopy()
定义为:
private void fileCopy(File source, File dest) throws IOException {
FileChannel inChannel = new FileInputStream(source).getChannel();
FileChannel outChannel = new FileOutputStream(dest).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null) inChannel.close();
if (outChannel != null) outChannel.close();
}
}
嗯 当然。如果这是应用程序中存在的数据库,则可以通过
Context.getDatabasePath()
获取对db文件的引用,并将数据库名称传递给它。从这里开始,它只是一个例行的文件复制操作:
//Get a reference to the database
File dbFile = mContext.getDatabasePath("mydb");
//Get a reference to the directory location for the backup
File exportDir = new File(Environment.getExternalStorageDirectory(), "myAppBackups");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File backup = new File(exportDir, dbFile.getName());
//Check the required operation String command = params[0];
//Attempt file copy
try {
backup.createNewFile();
fileCopy(dbFile, backup);
} catch (IOException e) {
/*Handle File Error*/
}
其中方法fileCopy()
定义为:
private void fileCopy(File source, File dest) throws IOException {
FileChannel inChannel = new FileInputStream(source).getChannel();
FileChannel outChannel = new FileOutputStream(dest).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null) inChannel.close();
if (outChannel != null) outChannel.close();
}
}
嗯 非常感谢SBerg413!这个很好用。谢谢。很好用。非常感谢SBerg413!这个很好用。谢谢。很好用。谢谢Devunwired!我希望我能将两个答案标记为正确,因为这也是一个很好的解决方案。绝对值得投一票。干杯我希望这能被标记为一个答案;它非常干净和精确。谢谢Devunwired!我希望我能将两个答案标记为正确,因为这也是一个很好的解决方案。绝对值得投一票。干杯我希望这能被标记为一个答案;它非常清晰和精确。我以什么方式没有回答你的问题?请不要再问问题——他有礼貌地让你的问题变得清晰。第一个问题是无意中发布的。这不是礼貌的问题,而是在我注意到我的浏览器在我写完问题之前贴出帖子之前,承认回答我问题的人的贡献。特别是因为,这两个问题都包含了值得认可的好答案。如果我是一个主持人,可以合并答案,我会的,但我不能,所以我没有。在我看来,删除另一个问题比提供两种不同的解决方案更糟糕。(一个是概念性的,第二个是函数代码示例)。但不要忽略让您(和其他人)改进问题或答案的按钮。:)肯定这里的人都很好,在偶然的帖子发布后不到两分钟就有了一个很好的答案。非常有效的观点,谢谢sarnold.:)我以什么方式没有回答你的问题?请不要再问问题——他有礼貌地让你的问题变得清晰。第一个问题是无意中发布的。这不是礼貌的问题,而是在我注意到我的浏览器在我写完问题之前贴出帖子之前,承认回答我问题的人的贡献。特别是因为,这两个问题都包含了值得认可的好答案。如果我是一个主持人,可以合并答案,我会的,但我不能,所以我没有。在我看来,删除另一个问题比提供两种不同的解决方案更糟糕。(一个是概念性的,第二个是函数代码示例)。但不要忽略让您(和其他人)改进问题或答案的按钮。:)肯定这里的人都很好,在偶然的帖子发布后不到两分钟就有了一个很好的答案。非常有效的观点,谢谢sarnold.:)