Is it possible to save database file to SD card?(是否可以将数据库文件保存到 SD 卡?)
问题描述
可能重复:
是否可以将数据库文件复制到 SD 卡?
我的 Android 手机上有一个数据库,我需要将信息存入 SD 卡.
I have a database on my Android phone, and I need to get the information onto an SD card.
是否可以将数据库文件以可读状态保存到 SD 卡上?我无法找到有关如何执行此操作的任何信息.我知道数据库的名称和字段等...
Is it possible to save the database file onto the SD card in a readable state? I haven't been able to find any information on how to do this. I know the name of the database, and fields etc...
我找到了一些展示如何保存到 SD 卡的示例,但并不完全符合我的需要.
I've found some examples that show how to save to SD cards, but not exactly what I need.
将数据库文件复制到 SD 卡的一些源代码将是完美的.
Some source code that copies the database file to an SD card would be perfect.
希望这个问题足够清楚.
Hopefully this question is clear enough.
推荐答案
是的.这是我使用的功能:
Yes. Here is the function that i use:
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 插件打开它.
For a project that I worked on, I put a menu option in the home screen that I could call this function from at any time. Then, I'd move the database to my desktop and open it up with the SQLite Manager plugin for FireFox.
这篇关于是否可以将数据库文件保存到 SD 卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否可以将数据库文件保存到 SD 卡?


基础教程推荐
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01