赞
踩
android文件永久存储一共有两个位置,内部存储和外部存储,这里的存储是指ROM(断电仍保持记忆),不是运行内存(RAM断电记忆丢失)。
这是由现在android手机的存储结构决定的,内部存储就是焊在手机里的闪存空间的一部分,该部分是app的私有空间,没有root的话,其他软件无法读写该部分空间。焊在手机里的闪存一般分为4g,16g,32g,64g,128g,android的手机一般是4g,16g,32g,64g为多,苹果的手机一般是16g,64g,128g三种选择。
外部存储是除了上面提到的app私有的空间外,以及系统占用的一部分焊在手机里的闪存空间外的部分存储空间,而且android手机由于手机用户自行选择插入sdcard这种情况造成了另一部分外部存储空间,如果像苹果那样不能自己插入sdcard,那么就不存在这样的外插sdcard外部存储这种情况.
android实际开发过程中会用到三个地方:
1、internal storage:内部储存,即app私有空间,绝对路径为 “/data/data/app-package-name/”,这部分物理上可以理解为焊在手机里的闪存空间的部分存储空间。
在该目录下会自动创建目录:files,cache,databases,lib,shared_prefs;这些目录因app中产生数据的存储需求而创建,app中没有产生存储需求就可能不产生这些文件夹中的部分。
2、external storage:外部存储空间:焊在手机里的闪存空间的部分,一般绝对路径为:/mnt/shell/emulated/0/(不同系统会有微小的区别,但是对于调用系统提供的函数获得路径而言,这样的区别对编程没有影响),一般该路径可以通过“file:///sdcard/xxx”获得,文件xxx就储存在“/mnt/shell/emulated/0/”目录下。
3、sdcard:外部存储空间,用户自己插入的sdcard,该空间不一定总是存在,所以使用之前需要程序判断是否插入了外部sdcard,外插sdcard根目录的绝对地址为:“/storage/sdcard1/”
为了简化操作,我们只将数据保存在内部存储空间 和 外部存储空间( 焊在手机里),我们可以只将“/data”目录下的存储空间认为是内部存储空间,/mnt/shell/emulated/0 目录下的空间认为是外部存储空间;而且外部存储空间的也分公用的,和私有的
(主要参考:http://www.androidchina.net/4106.html)
internal storage | /data | 由于内部存储空间有限,在开发中我们一般都是操作外部存储空间,Google官方建议我们App的数据应该存储在外部存储的私有 目录中该App的包名下,这样当用户卸载掉App之后,相关的数据会一并删除,如果你直接在/storage/sdcard目录下创建了一个应 用的文件夹,那么当你删除应用的时候,这个文件夹就不会被删除。 |
external storage | /mnt/shell/emulated/0 | 私有:/mnt/shell/emulated/0/Android/data/app_package_name/files /mnt/shell/emulated/0/Android/data/app_package_name/cache 公用:九大公有目录,比如DCIM、DOWNLOAD等这种系统为我们创建的文件夹 |
sdcard | /storage/sdcard1/ |
我们的app在使用空间的路径时,应该使用绝对路径,而不是相对路径,关于绝对路径和相对路径,参考:http://blog.csdn.net/androidwifi/article/details/17725989
下面这些函数的方法获得的都是
1、getFilesDir() = /data/data/com.gsh.gshow/files
2、getExternalFilesDir().getAbsolutePath() = /storage/emulated/0/Android/data/com.gsh.gshow/files
3、Environment.getDownloadCacheDirectory().getAbsolutePath() = /cache
4、Environment.getDataDirectory().getAbsolutePath() = /data
5、Environment.getExternalStorageDirectory().getAbsolutePath() = /storage/emulated/0 (->/mnt/shell/emulated/0)
6、Environment.getExternalStoragePublicDirectory("pub_test") = /storage/emulated/0
下面这个类操作的就是External storage:
- 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卡的完整空间大小,返回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) {
- // 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;
- }
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。