默认的sqlite数据库是放在/data/data/database目录下的,今天看腾讯云IM的demo发现再该路径下找不到它存放消息的数据库,找了下后发现居然是放在/data/data/files目录下的,虽然不知道为什么要放到这个目录,不过仔细想了下突然发觉假如把数据库放到非data目录下的话,就可以不借助contentprovider之类的方式实现跨应用访问数据库了,虽然安全性会降低,但在部分场景下还是会有所作用的。
阅读下了SqliteOpenHelper的源码,和数据库路径相关部分如下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
其实是调用了mContext的getDataBasePath方法,这个方法是在ContextImpl中实现的,再看下源码
@Override public File getDatabasePath(String name) { return validateFilePath(name, false); } private File validateFilePath(String name, boolean createDirectory) { File dir; File f; if (name.charAt(0) == File.separatorChar) { String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar)); dir = new File(dirPath); name = name.substring(name.lastIndexOf(File.separatorChar)); f = new File(dir, name); } else { dir = getDatabasesDir(); f = makeFilename(dir, name); } if (createDirectory && !dir.isDirectory() && dir.mkdir()) { FileUtils.setPermissions(dir.getPath(), FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, -1, -1); } return f; }
好吧,原来这么简单,原来Android本身已经实现了自定义路径的方法了,只要传入的path的第一个字符为"/"就行了。
总结下就一句话,只要传入自定义路径的完整路径就好了。。。。