android实现raw文件夹导入数据库代码

时间:2023-07-02 04:26:28 

有这样一道面试题:

如何将SQLite数据库(dictionary.db文件)与apk文件一起发布?


   答: 把这个文件放在/res/raw目录下即可。res\raw目录中的文件不会被压缩,这样可以直接提取该目录中的文件,会生成资源id。

那么如何把raw文件下面的数据库导入到安装的程序中的database目录下呢?


    public void imporDatabase() {
     //存放数据库的目录
     String dirPath="/data/data/com.hkx.wan/databases";
     File dir = new File(dirPath);
     if(!dir.exists()) {
      dir.mkdir();
     }
     //数据库文件
     File file = new File(dir, "abc.db");
     try {
      if(!file.exists()) {
       file.createNewFile();
      }
      //加载需要导入的数据库
      InputStream is = this.getApplicationContext().getResources().openRawResource(R.raw.db_weather);
      FileOutputStream fos = new FileOutputStream(file);
      byte[] buffere=new byte[is.available()];
      is.read(buffere);
      fos.write(buffere);
      is.close();
      fos.close();

     }catch(FileNotFoundException  e){
      e.printStackTrace();
     }catch(IOException e) {
      e.printStackTrace();
     }
    }

标签:android,raw文件夹,导入数据库
0
投稿

猜你喜欢

  • tk.Mybatis 插入数据获取Id问题

    2023-07-01 22:03:13
  • Android开机画面的具体修改方法

    2023-05-13 20:52:22
  • 深入多线程之:双向信号与竞赛的用法分析

    2022-02-17 06:54:49
  • C语言预处理预编译命令及宏定义详解

    2023-06-18 16:28:06
  • WPF应用启动慢的问题解决

    2021-09-07 23:14:01
  • C#、ASP.NET通用扩展工具类之LogicSugar

    2023-11-18 09:56:07
  • Spring中统一异常处理示例详解

    2022-08-01 07:09:24
  • java 验证用户是否已经登录与实现自动登录方法详解

    2021-10-21 13:49:50
  • SpringBoot实现启动类的存放位置

    2021-12-02 21:13:21
  • Android SearchView搜索控件使用方法详解

    2022-07-09 16:49:21
  • Android实现Window弹窗效果

    2022-12-07 21:48:16
  • Android 中View.onDraw(Canvas canvas)的使用方法

    2022-11-16 16:40:48
  • Android自定义照相机Camera出现黑屏的解决方法

    2023-01-02 14:45:40
  • Java为实体类动态添加属性的方法详解

    2023-09-01 05:31:43
  • 解决nacos升级spring cloud 2020.0无法使用bootstrap.yml的问题

    2021-12-02 19:44:29
  • Android实现上传文件功能的方法

    2022-01-18 11:01:02
  • Spring Boot启动过程完全解析(二)

    2022-06-17 14:40:59
  • 如何在IDE部署springboot项目(有swagger和无swagger都是一样的)到服务器或者虚拟机上的docker

    2023-09-01 00:33:25
  • Android应用启动速度优化

    2023-03-18 04:07:44
  • Android刮刮卡实现原理与代码讲解

    2021-08-06 13:50:42
  • asp之家 软件编程 m.aspxhome.com