首页 > 移动开发 > 将android程序中的数据库导出到SD卡
将android程序中的数据库导出到SD卡
孤风一剑
9月 02, 2014
219
- private void copyDBToSDcrad()
- {
- String DATABASE_NAME = “数据库文件名称”;
-
- String oldPath = “data/data/com.packagename/databases/” + DATABASE_NAME;
- String newPath = Environment.getExternalStorageDirectory() + File.separator + DATABASE_NAME;
-
- copyFile(oldPath, newPath);
- }
-
-
-
-
-
-
-
-
-
-
- public static void copyFile(String oldPath, String newPath)
- {
- try
- {
- int bytesum = 0;
- int byteread = 0;
- File oldfile = new File(oldPath);
- File newfile = new File(newPath);
- if (!newfile.exists())
- {
- newfile.createNewFile();
- }
- if (oldfile.exists())
- {
- InputStream inStream = new FileInputStream(oldPath);
- FileOutputStream fs = new FileOutputStream(newPath);
- byte[] buffer = new byte[1444];
- while ((byteread = inStream.read(buffer)) != –1)
- {
- bytesum += byteread;
- fs.write(buffer, 0, byteread);
- }
- inStream.close();
- }
- }
- catch (Exception e)
- {
- System.out.println(“复制单个文件操作出错”);
- e.printStackTrace();
-
- }
-
- }