Android开发中内存、内部存储、外部存储详解

来源:互联网 发布:东方财富网龙虎榜数据 编辑:程序博客网 时间:2024/05/01 03:13

手机是有两个内存的。2G和16G同时出现在一个手机中,2G是指运行内存,16G是指存储内存。

手机的内存,分两种,一个是存储内存,相当于电脑的硬盘,一般手机参数里超过4G的都是指这个。存储内存是可以扩展的。如果手机支持插卡就可以扩展。

另一个是运行内存,相当于电脑的内存条。这个是不能扩展的。基本上都在2GB以内,少量的可以达到3G或4G。


我们先来考虑这样一个问题:

打开手机设置,选择应用管理,选择任意一个App,然后你会看到两个按钮,一个是清除缓存,另一个是清除数据,那么当我们点击清除缓存的时候清除的是哪里的数据?当我们点击清除数据的时候又是清除的哪里的数据?读完本文相信你会有答案。

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

内存,我们在英文中称作memory,内部存储,我们称为InternalStorage,外部存储我们称为ExternalStorage,这在英文中本不会产生歧义,但是当我们翻译为中文之后,前两个都简称为内存,于是,混了。

那么究竟什么是内部存储什么是外部存储呢?

首先我们打开DDMS,有一个File Explorer,如下:
这里写图片描述

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

1.内部存储

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

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

1.data/data/包名/shared_prefs2.data/data/包名/databases3.data/data/包名/files4.data/data/包名/cache

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

2.外部存储

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

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

说到这里,我想大家应该已经可以分清楚什么是内部存储什么是外部存储了吧?好,分清楚之后我们就要看看怎么来操作内部存储和外部存储了。

3.操作存储空间

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

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

一目了然,什么是内部存储,什么是外部存储。
这里写图片描述

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

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

    /**     *     * @param type The type of files directory to return.  May be null for     * the root of the files directory or one of     * the following Environment constants for a subdirectory:     * {@link android.os.Environment#DIRECTORY_MUSIC},     * {@link android.os.Environment#DIRECTORY_PODCASTS},     * {@link android.os.Environment#DIRECTORY_RINGTONES},     * {@link android.os.Environment#DIRECTORY_ALARMS},     * {@link android.os.Environment#DIRECTORY_NOTIFICATIONS},     * {@link android.os.Environment#DIRECTORY_PICTURES}, or     * {@link android.os.Environment#DIRECTORY_MOVIES}.     *     * @return The path of the directory holding application files     * on external storage.  Returns null if external storage is not currently     * mounted so it could not ensure the path exists; you will need to call     * this method again when it is available.     *     * @see #getFilesDir     * @see android.os.Environment#getExternalStoragePublicDirectory     */      @Nullable      public abstract File getExternalFilesDir(@Nullable String type);  

它的注释非常多,我这里只列出其中一部分,我们看到,我们可以访问files文件夹下的Music文件夹、Movies文件夹等等好几种。

说到这里,我想大家对内部存储、外部存储该有了一个清晰的认识了吧。我们在开发中,不建议往内部存储中写太多的数据,毕竟空间有限。外部存储在使用的时候最好能够将文件存放在私有目录下,这样有利于系统维护,也避免用户的反感。

现在我们再来看看我们一开始提出的问题,当我们点击清除数据的时候清除的是哪里的数据呢?毫无疑问,当然是内部存储目录中相应的files和cache文件夹中的文件和外部存储中相应的files和cache文件夹中的文件,至于这些文件夹的路径我想你应该已经明白了。

最后提供一个工具类:

    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;                }           }      }  

手机内存的官方路径的:是String path=Environment.getExternalStorageDirectory().getAbsolutePath();因为安卓以不安全为由不鼓励应用将文件写入sd卡,自4.0以来api不提供sd卡的固定路径。sd卡路径根据厂家定制各不相同。比如三星的/storage/extSdCard,比如小米的/storage/sdcard1.调用getExternalStorageDirectory返回的也不会是外置sd卡目录而是手机内存。建议将文件存在手机内存或/data/data中。如果一定要获取sd卡路径。可以尝试在/mnt或/storage中遍历里面的文件夹,根据手机内存和sd卡一些细微的不同,比如sd卡上有LOST.DIR,找出sd卡路径。但是非官方的方法很难保证对于每一种定制的系统这种方法都奏效。

Android数据访问存储之内存读写

Android数据存储之内存读写的两大内存

1、手机内存数据读写

 getFileDir( ) :   得到当前app在手机内存存储数据的位置 /data/data/当前app包名/files getCacheDir( ) : 得到当前app在手机内存存储数据的位置/data/data/当前app包名/cache openFileInput(String name) : 直接得到/data/data/当前app包名/files/name文件的输入流 openFileOutput(String name,int mode) : 直接得到/data/data/当前app包名/files/name文件的输出流,mode为写入文件时的权限

2、sdcard数据读写

 Environment.getExternalStorageDirectory( ) :   得到当前app所在手机的sdcard位置/storage/sdcard      Environment.getExternalStoragePublicDirectory(String type) :   得到当前app所在手机的sdcard位置下的公共子文件夹/storage/sdcard/....    

Android数据存储之内存读写应用实例

布局文件 activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"          xmlns:tools="http://schemas.android.com/tools"          android:layout_width="match_parent"          android:layout_height="match_parent"          android:orientation="vertical"          android:paddingBottom="@dimen/activity_vertical_margin"          android:paddingLeft="@dimen/activity_horizontal_margin"          android:paddingRight="@dimen/activity_horizontal_margin"          android:paddingTop="@dimen/activity_vertical_margin"          tools:context=".MainActivity" >          <TextView              android:id="@+id/tv_main_memerysize"              android:layout_width="match_parent"              android:layout_height="80dip"              android:textSize="20sp"              android:textColor="#6666ff"              android:text="内存大小"/>          <TextView              android:id="@+id/tv_main_sdcard"              android:layout_width="match_parent"              android:layout_height="40dip"              android:textSize="20sp"              android:textColor="#ff6666"              android:text="SDCard存在否"/>          <TextView              android:id="@+id/tv_main_sdcardsize"              android:layout_width="match_parent"              android:layout_height="80dip"              android:textSize="20sp"              android:textColor="#ff6666"              android:text="SDCard内存大小"/>          <EditText               android:id="@+id/et_main_content"              android:layout_width="match_parent"              android:layout_height="0dip"              android:layout_weight="1"              android:textSize="20sp"              android:text="测试写入内存:\n将这里的文本信息保存到内存文件 testmemeryio.txt"/>          <Button               android:id="@+id/btn_main_writememery"              android:layout_width="match_parent"              android:layout_height="wrap_content"              android:text="写入内存"/>          <Button               android:id="@+id/btn_main_writesdcard"              android:layout_width="match_parent"              android:layout_height="wrap_content"              android:text="写入sdcard"/>      </LinearLayout>  

示例代码 MainActivity.Java

    package com.yihui.iomemery;      import java.io.FileOutputStream;      import java.io.IOException;      import android.annotation.SuppressLint;      import android.app.Activity;      import android.content.Context;      import android.os.Bundle;      import android.os.Environment;      import android.os.StatFs;      import android.text.format.Formatter;      import android.view.View;      import android.view.View.OnClickListener;      import android.widget.Button;      import android.widget.EditText;      import android.widget.TextView;      import android.widget.Toast;      public class MainActivity extends Activity implements OnClickListener {          private TextView tv_main_memerysize;          private TextView tv_main_sdcard;          private TextView tv_main_sdcardsize;          private EditText et_main_content;          private Button btn_main_writememery;          private Button btn_main_writesdcard;          private boolean sdcardMount = false;          @Override          protected void onCreate(Bundle savedInstanceState) {              super.onCreate(savedInstanceState);              setContentView(R.layout.activity_main);              tv_main_memerysize = (TextView) findViewById(R.id.tv_main_memerysize);              tv_main_sdcard = (TextView) findViewById(R.id.tv_main_sdcard);              tv_main_sdcardsize = (TextView) findViewById(R.id.tv_main_sdcardsize);              et_main_content = (EditText) findViewById(R.id.et_main_content);              btn_main_writememery = (Button) findViewById(R.id.btn_main_writememery);              btn_main_writesdcard = (Button) findViewById(R.id.btn_main_writesdcard);              btn_main_writememery.setOnClickListener(this);              btn_main_writesdcard.setOnClickListener(this);              //判断sdcard是否已安装              tv_main_sdcard.setText("sdcard未安装");              if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){                  sdcardMount = true;                  tv_main_sdcard.setText("sdcard已安装");              }              //得到sdcard和内存的大小及可用容量              if(sdcardMount){                  tv_main_sdcardsize.setText("sdcard" + getStorgeFileSize(Environment.getExternalStorageDirectory().getPath()));              }else {                  tv_main_sdcardsize.setText("sdcard总内存:0MB;  可用内存:0MB");              }              tv_main_memerysize.setText("手机" + getStorgeFileSize(Environment.getDataDirectory().getPath()));          }          /* 两个按钮的监听事件,将内容写入内存文件testmemeryio.txt  */          @Override          public void onClick(View v) {              String text = et_main_content.getText().toString();              boolean success = true;              switch (v.getId()) {                  case R.id.btn_main_writesdcard: //写入sdcard                      FileOutputStream fos = null;                      try {                          fos = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/m.txt");                          fos.write(text.getBytes("utf-8"));                          fos.flush();                      } catch (IOException e) {                          success = false;                          e.printStackTrace();                      } finally {                          if(fos != null){                              try {                                  fos.close();                                  fos = null;                              } catch (IOException e) {                                  e.printStackTrace();                              }                          }                      }                      Toast.makeText(MainActivity.this, success == true ? "写入sdcard文件成功" : "写入sdcard文件失败", 0).show();                      break;                  case R.id.btn_main_writememery: //写入内存                      FileOutputStream openFileOutput = null;                      try {                          //使用openFileOutput()函数,直接在/data/data/包名/files/目录下创建文件                          openFileOutput = openFileOutput("testmemeryio.txt", Context.MODE_PRIVATE);  //私有模式写文件                          openFileOutput.write(text.getBytes("utf-8"));                          openFileOutput.flush();                      } catch (IOException e) {                          success = false;                          e.printStackTrace();                      } finally {                          if(openFileOutput != null){                              try {                                  openFileOutput.close();                                  openFileOutput = null;                              } catch (IOException e) {                                  e.printStackTrace();                              }                          }                      }                      Toast.makeText(MainActivity.this, success == true ? "写入内存文件成功" : "写入内存文件失败", 0).show();                      break;                  default:                      break;              }          }          /* 取得内存文件空间大小及可用大小 */          @SuppressLint("NewApi") private String getStorgeFileSize(String path){              String fileSizeDesc = null;              StatFs statFs = new StatFs(path);                           //获得磁盘状态的对象              long blockSizeLong = statFs.getBlockSizeLong();             //获得磁盘一个扇区的大小              long blockCountLong = statFs.getBlockCountLong();           //获得磁盘空间总的扇区数              long availableBlocksLong = statFs.getAvailableBlocksLong(); //获得磁盘空间总的可用扇区数              fileSizeDesc = "总内存:" + Formatter.formatFileSize(MainActivity.this, blockSizeLong*blockCountLong)                           + "; 可用内存:" + Formatter.formatFileSize(MainActivity.this, blockSizeLong*availableBlocksLong);              return fileSizeDesc;          }      }  

注意:因为要读写SDCard,所以在项目里面的AndroidManifest.xml功能清单文件里面添加读写SDCard的权限

    ......      <uses-sdk          android:minSdkVersion="8"          android:targetSdkVersion="19" />      <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>  <!-- sdcard读权限 -->      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <!-- sdcard写权限 -->      ......  

运行效果:
这里写图片描述
在File Explorer下可以看到
/data/data/com.yihui.iomemery/files/testmemeryio.txt文件,且写入的内容就是UI上面文本框里面的内容

/storage/sdcard/m.txt文件,且写入的内容也是UI上面文本框里面的内容

Android数据存储之读写模式(读写权限)

Android是基于Linux操作系统的,所以Android的文件访问权限与Linux系统的文件访问权限是一致的,具体参考下图说明
这里写图片描述

1、Android数据访问权限实现方式

Context类的public abstract FileOutputStream openFileOutput(String name, int mode)

2、其中mode就是文件访问权限模式,主要4种模式

Context.MODE_PRIVATE:私有模式(默认模式),只能被应用本身和同一群组的人访问;写入的内容覆盖原文件内容

Context.MODE_APPEND: 追加模式也是私有模式,只能被应用本身和同一群组的人访问;如果文件存在就追加内容,如果文件不存在就新建文件并写入内容

Context.MODE_WORLD_READABLE: 所有人可读权限
Context.MODE_WORLD_WRITEABLE:所有人可写权限

也可通过多个组合同时拥有多个读写权限,Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE表示所有人可读+可写


Android实现内存中数据保存到sdcard的方法

public static void writeToSdCard(String s) {    try {      File dst = new File("/sdcard/test_sensor/" + mName + ".txt");      File parent = dst.getParentFile();      if(!parent.exists()) {        parent.mkdirs();      }      FileOutputStream outStream = new FileOutputStream(dst, true);      OutputStreamWriter writer = new OutputStreamWriter(outStream, "gb2312");      writer.write(s);      writer.write("\n");      writer.flush();      writer.close();// 记得关闭      outStream.close();    } catch (Exception e) {      Log.i("test result", "file write error");      e.printStackTrace();    }}
阅读全文
0 0
原创粉丝点击