缥帙各舒散,前后互相逾。这篇文章主要讲述Android中关于内部存储的一些重要函数相关的知识,希望能为你提供帮助。
一、简介
android中,你也可以通过绝对路径以java传统方式访问内部存储空间。但是以这种方式创建的文件是对私有,创建它的应用程序对该文件是可读可写,但是别的应用程序并不能直接访问它。不是所有的内部存储空间应用程序都可以访问,默认情况下只能访问“/data/data/你的应用程序的包名”这个路径下的文件。
Android中,你还可以使用Context对象的和来进行数据持久化存储的这种方式,你的数据文件将存储在内部存储空间的/data/data/你的应用程序的包名/files/目录下,无法指定更深一级的目录,而且默认是Context.MODE_PRIVATE模式,即别的应用程序不能访问它。你可以使用的int mode参数来让别的应用程序也能访问你的文件。
注意:保存在/data/data/你的应用程序的包名目录中文件,会在卸载你的应用程序时被删除掉。
二、Context中关于内部存储的重要函数
public abstract
File
getCacheDir
()
API Level 1Returns the absolute path to the application specific cache directory on
the filesystem. These files will be ones that get deleted first when
the device runs low on storage. There is no guarantee when these files
will be deleted.
Note: you should not
rely
on
the system deleting these files for you;
you should always have a
reasonable maximum, such as 1 MB, for the amount of space you consume
with cache files, and prune those files when exceeding that space.
存储空间剩余多少的情况,没有严格的标准保障。
注意:你不应该依赖系统来清理这些缓存文件,你应该对这些缓存文件占用的最大存储空间设定个最大值,比如是1M,当实际占用空间超过这个值时,你应该对这些缓存文件做相应的清理工作(prune)。
Returns
- Returns the path of the directory holding application cache files.
-
openFileOutput(String, int)
-
getFileStreamPath(String)
-
getDir(String, int)
- import android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- import android.util.Log;
- public class MainActivity extends Activity {
- final static String TAG="robin";
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Context context=this;
- String path=context.getCacheDir().getAbsolutePath();
- Log.i(TAG,"path:"+path);
- }
- }
- path:/data/data/com.lenovo/cache
:参数是指文件夹的访问权限而并不包括其子文件夹和文件的访问权限 Parametersname
Name of the directory to retrieve. This is a directory that is created as part of your application data. | |
mode |
Operating mode. Use 0 or
MODE_PRIVATE
for
the default operation,
MODE_WORLD_READABLE
and
MODE_WORLD_WRITEABLE
to
control permissions. |
---|
- Returns a File object for the requested directory. The directory will have been created if it does not already exist.
-
openFileOutput(String, int)
- File file=context.getDir("download", Context.MODE_PRIVATE);
- String path=file.getAbsolutePath();
- Log.i(TAG,"path:"+path);
- public abstract
File
getFileStreamPath
(String
name)Since:
API Level 1Returns the absolute path on the filesystem where a file created with
openFileOutput(String, int)
is stored.
ParametersnameThe name of the file for which you would like to get its path. - Returns an absolute path to the given file.
-
openFileOutput(String, int)
-
getFilesDir()
-
getDir(String, int)
- File file=context.getFileStreamPath("download");
- String path=file.getAbsolutePath();
- Log.i(TAG,"path:"+path);
- public abstract
File
getFilesDir
()Since:
API Level 1Returns the absolute path to the directory on the filesystem where files created with
openFileOutput(String, int)
are stored.
Returns- Returns the path of the directory holding application files.
openFileOutput(String, int)
getFileStreamPath(String)
getDir(String, int)
Parametersname The name of the file to open; can not contain path separators. - FileInputStream Resulting input stream.
FileNotFoundException openFileOutput(String, int)
fileList()
deleteFile(String)
FileInputStream(String)
Parametersname The name of the file to open; can not contain path separators. mode Operating mode. Use 0 or MODE_PRIVATE
for the default operation,MODE_APPEND
to append to an existing file,MODE_WORLD_READABLE
andMODE_WORLD_WRITEABLE
to control permissions.- FileOutputStream Resulting output stream.
FileNotFoundException MODE_APPEND 表示写文件时是追加模式,即从文件末开始写数据
MODE_PRIVATE
MODE_WORLD_READABLE
MODE_WORLD_WRITEABLE
openFileInput(String)
fileList()
deleteFile(String)
FileOutputStream(String)
Parametersname
The name of the file to delete; can not contain path separators. |
- True if the file was successfully deleted; else false.
openFileInput(String)
openFileOutput(String, int)
fileList()
delete()
Returns
- Array of strings naming the private files.
openFileInput(String)
openFileOutput(String, int)
deleteFile(String)
用File返回数据文件的根目录,返回的文件的路径为“/data”。该目录下的文件是只读。应用程序无法对该目录下的文件进行写操作。
public static File getDownloadCacheDirectory () Since: API Level 1Gets the android Download/Cache content directory.
【Android中关于内部存储的一些重要函数】用File返回缓存文件的根目录,返回的文件的路径为“/cache”。对于第三方应用程序。该目录下的文件是只读。第三方应用程序无法对该目录下的文件进行写操作。
public static File getRootDirectory () API Level 1 Gets the Android root directory.
用File返回Android系统文件的根目录,返回的文件的路径为“/system”。该目录下的文件是只读。应用程序无法对该目录下的文件进行写操作。
结束
推荐阅读
- Android02-控件
- Mybatis实现Mapper动态代理方式
- Android内存解析—从Linux系统内存逐步认识Android应用内存
- Spring MVC 学习笔记 handlerMapping和handlerAdapter
- 如何将Android Studio的项目上传到Github上
- 网页搜索之后的APP搜索
- Android实现App版本自动更新
- Android Studio 2.3.3 添加ksoap2的引用(拒绝网上其他的忽悠),也适用于添加其他Jar的引用
- Android 6.0 - 动态权限管理的解决方案(转)