您的位置:首页 > 移动开发 > Android开发

Android的内部存储和外部存储

2016-09-08 21:39 351 查看
在Android开发中我们常常听到这样几个概念,内存,内部存储,外部存储,很多人常常将这三个东西搞混,那么我们今天就先来详细说说这三个东西是怎么回事?

内存,我们在英文中称作memory,内部存储,我们称为InternalStorage,外部存储我们称为ExternalStorage。

首先我们打开DDMS,有一个File Explorer,如下:



这里有三个文件夹需要我们重视,一个是data,一个是mnt,一个是storage,我们下面就详细说说这三个文件夹。

内部存储

data文件夹就是我们常说的内部存储,当我们打开data文件夹之后(没有root的手机不能打开该文件夹),里边有两个文件夹值得我们关注,如下:



一个文件夹是app文件夹,还有一个文件夹就是data文件夹,app文件夹里存放着我们所有安装的app的apk文件,其实,当我们调试一个app的时候,可以看到控制台输出的内容,有一项是uploading …..就是上传我们的apk到这个文件夹,上传成功之后才开始安装。

data/app

另一个重要的文件夹就是data文件夹了,这个文件夹里边都是一些包名,打开这些包名之后我们会看到这样的一些文件:

data/data/包名/shared_prefs

data/data/包名/databases

data/data/包名/files

data/data/包名/cache

如果打开过data文件,应该都知道这些文件夹是干什么用的,我们在使用sharedPreferenced的时候,将数据持久化存储于本地,其实就是存在这个文件中的xml文件里,我们App里边的数据库文件就存储于databases文件夹中,还有我们的普通数据存储在files中,缓存文件存储在cache文件夹中,存储在这里的文件我们都称之为内部存储。

外部存储

外部存储才是我们平时操作最多的,外部存储一般就是我们上面看到的storage文件夹,当然也有可能是mnt文件夹,这个不同厂家有可能不一样。

一般来说,在storage文件夹中有一个sdcard文件夹(我们这里只考虑内置SD),这个文件夹中的文件又分为两类,一类是公有目录,还有一类是私有目录,其中的公有目录有九大类,比如DCIM、DOWNLOAD等这种系统为我们创建的文件夹,私有目录就是Android这个文件夹,这个文件夹打开之后里边有一个data文件夹,打开这个data文件夹,里边有许多包名组成的文件夹。

storage/sdcard/Android/data/包名/files

storage/sdcard/Android/data/包名/cache

从Android 1.0开始,外部存储写操作受权限WRITE_EXTERNAL_STORAGE保护。

从Android 4.1开始,外部存储读操作受权限READ_EXTERNAL_STORAGE保护。

从Android 4.4开始,应用可以管理在它外部存储上的特定包名目录,而不用获取WRITE_EXTERNAL_STORAGE权限。

比如,一个包名为com.example.foo的应用,可以自由访问外存上的Android/data/com.example.foo/目录。

注:外部存储对数据提供的保护较少,所以系统不应该存储敏感数据在外部存储上。配置信息等文件应该存储在内部存储中,这样它们可以被有效地保护。

访问存储空间

首先,经过上面的分析,大家已经明白了,什么是内部存储,什么是外部存储,以及这两种存储方式分别存储在什么位置,一般来说,我们不会自己去操作内部存储空间,没有root权限的话,我们也没法操作内部存储空间,事实上内部存储主要是由系统来维护的。不过在代码中我们是可以访问到这个文件夹的。由于内部存储空间有限,在开发中我们一般都是操作外部存储空间,Google官方建议我们App的数据应该存储在外部存储的私有目录中该App的包名下,这样当用户卸载掉App之后,相关的数据会一并删除,如果你直接在/storage/sdcard目录下创建了一个应用的文件夹,那么当你删除应用的时候,这个文件夹就不会被删除。

经过以上的介绍,我们可以总结出下面一个表格:



如果按照路径的特征,我们又可以将文件存储的路径分为两大类,一类是路径中含有包名的,一类是路径中不含有包名的,含有包名的路径,因为和某个App有关,所以对这些文件夹的访问都是调用Context里边的方法,而不含有包名的路径,和某一个App无关,我们可以通过Environment中的方法来访问。如下图:



大家看到,有包名的路径我们都是调用Context中的方法来获得,没有包名的路径,我们直接调用Environment中的方法获得,那么其中有两个方法需要传入一个String类型的参数,这个参数我们使用了Environment中的常量,参数的意思是我们要访问这个路径下的哪个文件夹,主要有以下常量:

* Environment.DIRECTORY_MUSIC
* Environment.DIRECTORY_PODCASTS
* Environment.DIRECTORY_RINGTONES
* Environment.DIRECTORY_ALARMS
* Environment.DIRECTORY_NOTIFICATIONS
* Environment.DIRECTORY_PICTURES
* Environment.DIRECTORY_MOVIES


当然这里我们也可以传入自己创建的文件夹名称,如:

Environment.getExternalStoragePublicDirectory("storage_test"));


Environment API的目录

getRootDirectory():Android的根目录。

getDataDirectory():用户数据目录。

getDownloadCacheDirectory():下载缓存内容目录。

getExternalStorageDirectory():主要的外部存储目录。

但是这个目录很可能当前不能访问,比如这个目录被用户的PC挂载,或者从设备中移除,或者其他问题发生,你可以通过getExternalStorageState()来获取当前状态。

getExternalStoragePublicDirectory(String type):公有目录。

写这个路径需要 WRITE_EXTERNAL_STORAGE权限,读需要 READ_EXTERNAL_STORAGE权限,当然写权限默认包含了读权限。

isExternalStorageEmulated():设备的外存是否是用内存模拟的。

isExternalStorageRemovable():设备的外存是否是可以拆卸的,比如SD卡。

Context API中的目录

这些目录都是属于应用的,当应用被卸载的时候,里面的内容都会被移除。

getExternalFilesDir(String type):应用在外部存储上的文件目录。

getExternalCacheDir():应用的在外部存储上的缓存目录。

Android 4.4这两个方法不需要读写权限,是针对于本应用来说,如果要访问其他应用的相关目录,还是需要声明读写权限。

getFilesDir():应用在内部存储上的文件目录。

getCacheDir():应用的在内部存储上的缓存目录。

我们写个Demo将上面获取的路径分别log打出来:

注:外部存储不同厂家实现可能会不一样,可能是storage文件夹,也可能是mnt文件夹。

这里用的是华为荣耀i7:



操作内部存储

如果是要创建一个文件在内部存储,如下:

File testfile = new File(context.getFilesDir(), "hxtest");
if (!testfile.exists()) {
testfile.mkdir();
}


安卓还为我们提供了一个简便方法 openFileOutput()来读写应用在内部存储空间上的文件,下面是一个向文件中写入文本的例子:

String string = "Hello world!";
FileOutputStream outputStream;
try{
outputStream = openFileOutput("hxtest", Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch(Exception e) {
e.printStackTrace();
}


内部存储的其他一些操作:

A.列出所有的已创建的文件,这个可能不容易想到,Context居然有这样的方法。

String[] files = context.fileList();
showlog("file size is "+ files.length);
for(String file : files) {
showlog("file is "+ file);
}


B.删除文件,能创建就要能够删除,当然也会提供了删除文件的接口,它也非常简单,只需要提供文件名。

if(context.deleteFile("hxtest")) {
showlog("delete file sucessfully");
} else {
showlog("failed to deletefile");
}


C.创建一个目录,需要传入目录名称,它返回 一个文件对象用到操作路径

File workDir = context.getDir("hxDir", Context.MODE_PRIVATE);
showlog("workdir "+ workDir.getAbsolutePath());




总结一下文件相关操作,可以得出以下三个特点:

1. 文件操作只需要向函数提供文件名,所以程序自己只需要维护文件名即可。

2. 不用自己去创建文件对象和输入、输出流,提供文件名就可以返回File对象或输入输出流。

3. 对于路径操作返回的都是文件对象。

操作外部存储

这里我提供给大家一个操作外部存储的工具类:

public class SDCardHelper {

// 判断SD卡是否被挂载
public static boolean isSDCardMounted() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}

// 获取SD卡的根目录
public static String getSDCardBaseDir() {
if (isSDCardMounted()) {
return Environment.getExternalStorageDirectory().getAbsolutePath();
}
return null;
}

// 获取SD卡的完整空间大小,返回MB
public static long getSDCardSize() {
if (isSDCardMounted()) {
StatFs fs = new StatFs(getSDCardBaseDir());
long count = fs.getBlockCountLong();
long size = fs.getBlockSizeLong();
return count * size / 1024 / 1024;
}
return 0;
}

// 获取SD卡的剩余空间大小
public static long getSDCardFreeSize() {
if (isSDCardMounted()) {
StatFs fs = new StatFs(getSDCardBaseDir());
long count = fs.getFreeBlocksLong();
long size = fs.getBlockSizeLong();
return count * size / 1024 / 1024;
}
return 0;
}

// 获取SD卡的可用空间大小
public static long getSDCardAvailableSize() {
if (isSDCardMounted()) {
StatFs fs = new StatFs(getSDCardBaseDir());
long count = fs.getAvailableBlocksLong();
long size = fs.getBlockSizeLong();
return count * size / 1024 / 1024;
}
return 0;
}

// 往SD卡的公有目录下保存文件
public static boolean saveFileToSDCardPublicDir(byte[] data, String type, String fileName) {
BufferedOutputStream bos = null;
if (isSDCardMounted()) {
File file = Environment.getExternalStoragePublicDirectory(type);
try {
bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
bos.write(data);
bos.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}

// 往SD卡的自定义目录下保存文件
public static boolean saveFileToSDCardCustomDir(byte[] data, String dir, String fileName) {
BufferedOutputStream bos = null;
if (isSDCardMounted()) {
File file = new File(getSDCardBaseDir() + File.separator + dir);
if (!file.exists()) {
file.mkdirs();// 递归创建自定义目录
}
try {
bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
bos.write(data);
bos.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}

// 往SD卡的私有Files目录下保存文件
public static boolean saveFileToSDCardPrivateFilesDir(byte[] data, String type, String fileName, Context context) {
BufferedOutputStream bos = null;
if (isSDCardMounted()) {
File file = context.getExternalFilesDir(type);
try {
bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
bos.write(data);
bos.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}

// 往SD卡的私有Cache目录下保存文件
public static boolean saveFileToSDCardPrivateCacheDir(byte[] data, String fileName, Context context) {
BufferedOutputStream bos = null;
if (isSDCardMounted()) {
File file = context.getExternalCacheDir();
try {
bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
bos.write(data);
bos.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}

// 保存bitmap图片到SDCard的私有Cache目录
public static boolean saveBitmapToSDCardPrivateCacheDir(Bitmap bitmap, String fileName, Context context) {
if (isSDCardMounted()) {
BufferedOutputStream bos = null;
// 获取私有的Cache缓存目录
File file = context.getExternalCacheDir();
try {
bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
if (fileName != null
&& (fileName.contains(".png") || fileName.contains(".PNG"))) {                            bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
} else {                              bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
}
bos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
} else {
return false;
}
}

// 从SD卡获取文件
public static byte[] loadFileFromSDCard(String fileDir) {
BufferedInputStream bis = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
bis = new BufferedInputStream(new FileInputStream(new File(fileDir)));
byte[] buffer = new byte[8 * 1024];
int c = 0;
while ((c = bis.read(buffer)) != -1) {
baos.write(buffer, 0, c);
baos.flush();
}
return baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
baos.close();
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

// 从SDCard中寻找指定目录下的文件,返回Bitmap
public Bitmap loadBitmapFromSDCard(String filePath) {
byte[] data = loadFileFromSDCard(filePath);
if (data != null) {
Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
if (bm != null) {
return bm;
}
}
return null;
}

// 获取SD卡公有目录的路径
public static String getSDCardPublicDir(String type) {
return Environment.getExternalStoragePublicDirectory(type).toString();
}

// 获取SD卡私有Cache目录的路径
public static String getSDCardPrivateCacheDir(Context context) {
return context.getExternalCacheDir().getAbsolutePath();
}

// 获取SD卡私有Files目录的路径
public static String getSDCardPrivateFilesDir(Context context, String type) {
return context.getExternalFilesDir(type).getAbsolutePath();
}

// 判断SD卡文件是否存在
public static boolean isFileExist(String filePath) {
File file = new File(filePath);
return file.isFile();
}

// 从sdcard中删除文件
public static boolean removeFileFromSDCard(String filePath) {
File file = new File(filePath);
if (file.exists()) {
try {
file.delete();
return true;
} catch (Exception e) {
return false;
}
} else {
return false;
}
}
}


Demo下载地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: