导入外部的sqlite数据库,运行时报错打不开数据库.

private SQLiteDatabase openDatabase(String dbfile) {
    try {
        if (!(new File(dbfile).exists())) {
            //判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库
       File f = new File(DB_PATH);
            // 如 database 目录不存在,新建该目录
            if (!f.exists()) {
                f.mkdir();
            }
            try {
                InputStream is = this.context.getResources().openRawResource(
                        R.raw.onedic);
                // 输出流
                OutputStream os = new FileOutputStream(dbfile);
                // 文件写入
                byte[] buffer = new byte[1024];
                int length;
                while ((length = is.read(buffer)) > 0) {
                    os.write(buffer, 0, length);
                }
                // 关闭文件流
                os.flush();
                os.close();
                is.close();

        }catch (Exception e){
                e.printStackTrace();
            }
        }
            SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,
                    null);
        return db;
    } catch (Exception e) {
        Log.e("Database", "File not found");
        e.printStackTrace();
    }
    return null;
}
这个是我把在assets文件下的数据库写入手机里面的部分代码
然后就报错:
![图片描述][1]

图片描述

阅读 3.9k
1 个回答

为什么不直接读,而要先进行一下文件读写操作呢

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题