Android内部存储和外部存储
Android内部存储和外部存储
在Android中进行数据的保存有五种方式:
1:通过Shared Preferences :以键值对的方式保存在data/data/包名/shared_prefs文件夹下;2:以文件存储方式保存到指定的文件夹下(可以保存到内部存储中或者保存到外部存储里)3: SQLite数据库存储数据4:使用ContentProvider存储数据5:网络存储数据
这篇文章我们就说一下内部存储和外部存储;
内部存储(InternalStorage)
内部存储包含了两个部分:RAM和ROM
RAM:(random access memory)即是“随机存储器”;相当于我们所说的内存,用于暂时存放CPU中的运算数据,以及与硬盘等外部存储器交换的数据。掉电之后,数据就会消失。
ROM:( read-only memory)即为:只读存储器;相当于我们所说的硬盘,一般是装入整机前事先写好的,整机工作过程中只能读出,而不像随机存储器那样能快速地、方便地加以改写。ROM所存数据稳定 ,断电后所存数据也不会改变;其结构较简单,读出较方便,因而常用于存储各种固定程序和数据”。
我们打开data/data/包名文件夹:
里面有五个文件夹:
1.data/data/包名/shared_prefs2.data/data/包名/databases3.data/data/包名/files4.data/data/包名/cache5.data/data/包名/lib
我们在使用sharedPreferenced的时候,将数据持久化存储于本地,其实就是存在这个文件中的xml文件里,我们App里边的数据库文件就存储于databases文件夹中,还有我们的普通数据存储在files中,缓存文件存储在cache文件夹中,存储在这里的文件我们都称之为内部存储。lib文件夹存储的引用的库文件。
我们再看后面除了创建时间还有一行(drwxrwxrwx)这是什么意思呢?
我们知道在Android中,每一个应用是一个独立的用户:
- 第1位:d表示文件夹,-表示文件
- 第2-4位:rwx,表示这个文件的拥有者用户(owner)对该文件的权限
- r:读
- w:写
- x:执行
- 第5-7位:rwx,表示跟文件拥有者用户同组的用户(grouper)对该文件的权限
- 第8-10位:rwx,表示其他用户组的用户(other)对该文件的权限
那么我们该如何把文件写入到内部存储当中呢:
直接开启文件输出流写数据
//持久化保存数据 File file = new File("data/data/包名/文件名"); FileOutputStream fos = new FileOutputStream(file); fos.write((string).getBytes()); fos.close();
* 读取数据前先检测文件是否存在
if(file.exists())
* 读取保存的数据,也是直接开文件输入流读取
File file = new File("data/data/包名/文件名"); FileInputStream fis = new FileInputStream(file); //把字节流转换成字符流 BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String text = br.readLine();
这种写法路径很容易出错,我们可以采用api获得这些路径:
- getFilesDir()得到的file对象的路径是data/data/包名/files
- 存放在这个路径下的文件,只要你不删,它就一直在
- getCacheDir()得到的file对象的路径是data/data/包名/cache
- 存放在这个路径下的文件,当内存不足时,有可能被删除
*在这里要说一下:我们在系统管理应用界面的清除缓存,会清除cache文件夹下的东西,清除数据,会清除整个包名目录下的东西
应用只能在自己的包名目录下创建文件,不能到别人家去创建。
Android还提供了一个api:openFileOutput()来读写应用在内部存储空间上的文件
它有四种模式:
- MODE_PRIVATE: -rw-rw—-
- MODE_APPEND: -rw-rw—-
- MODE_WORLD_WRITEABLE: -rw-rw–w-
- MODE_WORLD_READABLE:-rw-rw-r–
清除缓存和获取缓存大小工具类:
public class DataCleanManager { /** * * 清除本应用内部缓存(/data/data/com.xxx.xxx/cache) * * * * @param context */ public static void cleanInternalCache(Context context) { deleteFilesByDirectory(context.getCacheDir()); } /** * * 清除本应用所有数据库(/data/data/com.xxx.xxx/databases) * * * * @param context */ public static void cleanDatabases(Context context) { deleteFilesByDirectory(new File("/data/data/" + context.getPackageName() + "/databases")); } /** * * 清除本应用SharedPreference(/data/data/com.xxx.xxx/shared_prefs) * * * @param context */ public static void cleanSharedPreference(Context context) { deleteFilesByDirectory(new File("/data/data/" + context.getPackageName() + "/shared_prefs")); } /** * * 按名字清除本应用数据库 * * * * @param context * @param dbName */ public static void cleanDatabaseByName(Context context, String dbName) { context.deleteDatabase(dbName); } /** * * 清除/data/data/com.xxx.xxx/files下的内容 * * * * @param context */ public static void cleanFiles(Context context) { deleteFilesByDirectory(context.getFilesDir()); } /** * * 清除外部cache下的内容(/mnt/sdcard/android/data/com.xxx.xxx/cache) * * @param context */ public static void cleanExternalCache(Context context) { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { deleteFilesByDirectory(context.getExternalCacheDir()); } } /** * * 清除自定义路径下的文件,使用需小心,请不要误删。而且只支持目录下的文件删除 * * * * @param filePath * */ public static void cleanCustomCache(String filePath) { deleteFilesByDirectory(new File(filePath)); } /** * * 清除本应用所有的数据 * * * * @param context * @param filepath */ public static void cleanApplicationData(Context context, String... filepath) { cleanInternalCache(context); cleanExternalCache(context); cleanDatabases(context); cleanSharedPreference(context); cleanFiles(context); if (filepath == null) { return; } for (String filePath : filepath) { cleanCustomCache(filePath); } } /** * * 删除方法 这里只会删除某个文件夹下的文件,如果传入的directory是个文件,将不做处理 * * * * @param directory */ private static void deleteFilesByDirectory(File directory) { if (directory != null && directory.exists() && directory.isDirectory()) { for (File item : directory.listFiles()) { item.delete(); } } } // 获取文件 //Context.getExternalFilesDir() --> SDCard/Android/data/你的应用的包名/files/ 目录,一般放一些长时间保存的数据 //Context.getExternalCacheDir() --> SDCard/Android/data/你的应用包名/cache/目录,一般存放临时缓存数据 public static long getFolderSize(File file) throws Exception { long size = 0; try { File[] fileList = file.listFiles(); for (int i = 0; i < fileList.length; i++) { // 如果下面还有文件 if (fileList[i].isDirectory()) { size = size + getFolderSize(fileList[i]); } else { size = size + fileList[i].length(); } } } catch (Exception e) { e.printStackTrace(); } return size; } /** * 删除指定目录下文件及目录 * * @param deleteThisPath * @param filepath * @return */ public static void deleteFolderFile(String filePath, boolean deleteThisPath) { if (!TextUtils.isEmpty(filePath)) { try { File file = new File(filePath); if (file.isDirectory()) {// 如果下面还有文件 File files[] = file.listFiles(); for (int i = 0; i < files.length; i++) { deleteFolderFile(files[i].getAbsolutePath(), true); } } if (deleteThisPath) { if (!file.isDirectory()) {// 如果是文件,删除 file.delete(); } else {// 目录 if (file.listFiles().length == 0) {// 目录下没有文件或者目录,删除 file.delete(); } } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 格式化单位 * * @param size * @return */ public static String getFormatSize(double size) { double kiloByte = size / 1024; if (kiloByte < 1) { return size + "Byte"; } double megaByte = kiloByte / 1024; if (megaByte < 1) { BigDecimal result1 = new BigDecimal(Double.toString(kiloByte)); return result1.setScale(2, BigDecimal.ROUND_HALF_UP) .toPlainString() + "KB"; } double gigaByte = megaByte / 1024; if (gigaByte < 1) { BigDecimal result2 = new BigDecimal(Double.toString(megaByte)); return result2.setScale(2, BigDecimal.ROUND_HALF_UP) .toPlainString() + "MB"; } double teraBytes = gigaByte / 1024; if (teraBytes < 1) { BigDecimal result3 = new BigDecimal(Double.toString(gigaByte)); return result3.setScale(2, BigDecimal.ROUND_HALF_UP) .toPlainString() + "GB"; } BigDecimal result4 = new BigDecimal(teraBytes); return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB"; } public static String getCacheSize(File file) throws Exception { return getFormatSize(getFolderSize(file)); } }
外部存储(External storage)
Internal storage是Internal memory的一个分区,而External storage较为复杂,它分为两个部分:Primary external storage(可翻译为基础外部存储)和Secondary external storage(可翻译为附加外部存储).其中Primary external storage也是Internal memory的一个分区,而Secondary external storage则是sd card. 显然,这是一个历史遗留问题,在早期Android版本上,由于flash card价格昂贵,所以采用sd card进行拓展,但是后面随着flash card价格下降,并且sd card带来很多问题(比如卡顿,数据容易丢失),所以Google和手机产商都在逐步取消sd card,到现在大部分手机已经没有sd card了,所以现在的External storage基本可以理解成Internal memory的一个特殊分区。
sd卡存储
sd卡路径:
- 2.2之前,sd卡路径:sdcard
- 4.3之前,sd卡路径:mnt/sdcard
4.3开始,sd卡路径:storage/sdcard
最简单的打开sd卡的方式
File file = new File("sdcard/文件名");
写sd卡需要权限
读sd卡,在4.0之前不需要权限,4.0之后可以设置为需要
使用api获得sd卡的真实路径,部分手机品牌会更改sd卡的路径
Environment.getExternalStorageDirectory()
判断sd卡是否准备就绪
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
sd卡的相关操作工具类
public class SDCardHelper { // 判断SD卡是否被挂载 public static boolean isSDCardMounted() { // return Environment.getExternalStorageState().equals("mounted"); return Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED);}// 获取SD卡的根目录public static String getSDCardBaseDir() { if (isSDCardMounted()) { return Environment.getExternalStorageDirectory().getAbsolutePath(); } return null;}// 获取SD卡的完整空间大小,返回MBpublic 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) { // TODO Auto-generated catch block 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) { // TODO Auto-generated catch block 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) { // TODO Auto-generated catch block 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) { // TODO Auto-generated catch block 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(); } 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; } }}
更多相关文章
- 【转】Android深入探究笔记之三 -- Intent (隐式意图和显示意图)
- Android(安卓)之使用ContentProvider(内容提供者)共享数据
- Android(安卓)5.1编译大于2G的OTA包是会出 Android(安卓)Overflo
- 19個好用的Android開發工具(Written By: ventans Leo)
- Android多语言适配values文件夹命名
- Android(安卓)工程中各种文件的介绍
- 【Android】xml文件里面出现unbound prefix的问题
- Android(安卓)SQLite数据库增删改查操作
- 完美解决 No IDEA annotations attached to the JDK 1.8 (C:\Pr