在Context api中,有几组函数,如getExternalCacheDir(),getDir(),getCacheDir()等,返回的都是一个File对象。这些函数在软件的缓存管理中,十分有用。现对照着api对这些函数的使用场景和区别作下总结。
Context.getDatabasePath(String name) : Context.openOrCreateDataBase会创建一个数据库。而getDatabasePath就是返回文件系统上此数据库的绝对路径。参数name为数据库的名字;
- Context.getDir(String name,int mode) API level 1
- Context.getCacheDir() API level 1
- Context.getExternalCacheDir() API level 8
- Context.getExternalFilesDir() API level 8
- Context.getFilesDir() API level 1
好吧,看这5个函数略晕。莫晕,api给了很详细的解释。
先看下getDir(String name,int mode)的用法吧。
获取一个文件的路径(如果不存在就创建),应用程序可以在这个路径里放置一些数据文件。通过这个函数返回的file对象,我们可以创建或者读取这个路径里的文件。但是只有我们自己的应用程序可以访问这些文件。嘿,看来我们可以在这个函数返回的文件路径里放点自己的“私房钱”。还有,我们可以通过参数mode来设置文件的读取权限。再加一句,返回的这个文件,可以是通过
Context.openFileOutput(String name,int mode)创建的。
再来看context另外两个函数,getCacheDir()和getExternalCacheDir().这俩哥们,虽说getExternalCacheDir比getCacheDir就多了个external,这待遇就差很多了。
既然是兄弟,就有很多共同点。首先,我们的应用都可以往这两个函数返回的路径里放入缓存文件,而且这些文件大小还有限制!比如说1mb,要是缓存文件超过1mb了,那不行,得把多余的数据删掉,这些删除操作不能依赖系统噢,得自己搞。当软件被删掉的时候,这些缓存文件也得被删掉。系统内存不足的时候,这些缓存文件也会最先被删除。
不同点,那getExternalCacheDir()就得有一把泪啊。首先,由于系统不经常监督sd卡的可用大小为多少,放在sd卡的这些缓存文件就可能没有删除。还有当sd拔出,或者手机连着电脑,sd装载的时候,就不能再读写这些文件了。所以使用之前,得问问sd的状态。所以别说自己待遇差,自己不靠谱先!还有就是写在sd卡的缓存文件,是没有安全措施保存的。。别的应用也可以看的。。
所以说getCacheDir()这个函数是写在系统内部的,有防盗门守护着。缓存文件可以安心的放,但是注意,别放太多,超过限制要把多余得舍弃掉。而getExternalCacheDir是将文件放在存储卡里得,门户大开,别的应用可以读写。不过一般的缓存文件,跟用户信息无关的么,还好。
再说说最后两个函数:getFilesDir()和getExternalFilesDir().
getFilesDir()是跟openFileOutput()配对使用的。对,就是专门返回使用openFileOutput()创建的文件。这个跟getDir()有些类似,不过getDir()出了会干这个,还会帮你创建文件呢。
getExternalFilesDir()跟getExternalCacheDir()这个哥差不多。都很开放,什么你的我的,那是大家的,不同的应用也可以读写这些文件。然后sd卸载了,缓存文件也就都没了。还有一样很刁,哪怕是图片放在这个路径里,系统的图片库也是看不到这个图片的。哭,你要是想把图片放到媒体库里,还得执行个插入媒体库操作。。看个代码段吧。
void createExternalStoragePrivatePicture() { // Create a path where we will place our picture in our own private // pictures directory. Note that we don't really need to place a // picture in DIRECTORY_PICTURES, since the media scanner will see // all media in these directories; this may be useful with other // media types such as DIRECTORY_MUSIC however to help it classify // your media for display to the user. File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES); File file = new File(path, "DemoPicture.jpg"); try { // Very simple code to copy a picture from the application's // resource into the external file. Note that this code does // no error checking, and assumes the picture is small (does not // try to copy it in chunks). Note that if external storage is // not currently mounted this will silently fail. InputStream is = getResources().openRawResource(R.drawable.balloons); OutputStream os = new FileOutputStream(file); byte[] data = new byte[is.available()]; is.read(data); os.write(data); is.close(); os.close(); // Tell the media scanner about the new file so that it is // immediately available to the user. MediaScannerConnection.scanFile(this, new String[] { file.toString() }, null, new MediaScannerConnection.OnScanCompletedListener() { public void onScanCompleted(String path, Uri uri) { Log.i("ExternalStorage", "Scanned " + path + ":"); Log.i("ExternalStorage", "-> uri=" + uri); } }); } catch (IOException e) { // Unable to create file, likely because external storage is // not currently mounted. Log.w("ExternalStorage", "Error writing " + file, e); } } void deleteExternalStoragePrivatePicture() { // Create a path where we will place our picture in the user's // public pictures directory and delete the file. If external // storage is not currently mounted this will fail. File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES); if (path != null) { File file = new File(path, "DemoPicture.jpg"); file.delete(); } } boolean hasExternalStoragePrivatePicture() { // Create a path where we will place our picture in the user's // public pictures directory and check if the file exists. If // external storage is not currently mounted this will think the // picture doesn't exist. File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES); if (path != null) { File file = new File(path, "DemoPicture.jpg"); return file.exists(); } return false; }