android 获取外部存储空间的那点事

本文参考地址:http://www.cnblogs.com/mengdd/p/3742623.html

上周老板的手机进行了一次插拔sd卡,app发图片到车友圈就罢工了,我调试了一下代码,我发现是路径的问题。

原来app路径是这样写的:

Environment.getExternalStorageDirectory()+"/coolcar/CoolcarImg"
我觉得没有问题啊,于是我改了一下路径:

Environment.getExternalStorageDirectory()
然后运行一下,就没有问题了,但是如果再次插拔内存卡,会不会发生问题,说实话,我真不知道。这个问题也一直困扰我,为了一探究竟,我打开了google的android官方文档,特意查看了一下这个方法,如下:

public static File getExternalStorageDirectory ()
Added in  API level 1

Return the primary external storage directory. This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened. You can determine its current state with getExternalStorageState().

Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

On devices with multiple users (as described by UserManager), each user has their own isolated external storage. Applications only have access to the external storage for the user they're running as.

In devices with multiple "external" storage directories, this directory represents the "primary" external storage that the user will interact with. Access to secondary storage is available through

Applications should not directly use this top-level directory, in order to avoid polluting the user's root namespace. Any files that are private to the application should be placed in a directory returned by Context.getExternalFilesDir, which the system will take care of deleting if the application is uninstalled. Other shared files should be placed in one of the directories returned by getExternalStoragePublicDirectory(String).

Writing to this path requires the WRITE_EXTERNAL_STORAGE permission, and starting in read access requires the READ_EXTERNAL_STORAGE permission, which is automatically granted if you hold the write permission.

Starting in KITKAT, if your application only needs to store internal data, consider using getExternalFilesDir(String) or getExternalCacheDir(), which require no permissions to read or write.

This path may change between platform versions, so applications should only persist relative paths.

Here is an example of typical code to monitor the state of external storage:

BroadcastReceiver mExternalStorageReceiver;
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;

void updateExternalStorageState() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        mExternalStorageAvailable = mExternalStorageWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        mExternalStorageAvailable = true;
        mExternalStorageWriteable = false;
    } else {
        mExternalStorageAvailable = mExternalStorageWriteable = false;
    }
    handleExternalStorageState(mExternalStorageAvailable,
            mExternalStorageWriteable);
}

void startWatchingExternalStorage() {
    mExternalStorageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("test", "Storage: " + intent.getData());
            updateExternalStorageState();
        }
    };
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
    filter.addAction(Intent.ACTION_MEDIA_REMOVED);
    registerReceiver(mExternalStorageReceiver, filter);
    updateExternalStorageState();
}

void stopWatchingExternalStorage() {
    unregisterReceiver(mExternalStorageReceiver);
}
我英文不好,我打开百度翻译,边看边翻译,大概懂了一点,好像google不推荐使用
Environment.getExternalStorageDirectory()

         于是,我再搜索了一下,就参考了另外一篇博客,看了之后,就明白许多了。内容如下:

Android的外部存储

  Android支持外部存储(case-insensitive filesystem with immutable POSIX permission classes and modes)。

  外部存储可以通过物理介质提供(如SD卡),也可以通过将内部存储中的一部分封装而成,设备可以有多个外部存储实例。

 

访问外部存储的权限

  从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/目录。

 

  外部存储对数据提供的保护较少,所以系统不应该存储敏感数据在外部存储上。

  特别地,配置和log文件应该存储在内部存储中,这样它们可以被有效地保护。

 

  对于多用户的情况,一般每个用户都会有自己独立的外部存储,应用仅对当前用户的外部存储有访问权限。

 

Environment API的目录

  getDataDirectory():用户数据目录。

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

 

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

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

  还有多用户或者多外部存储的情况,此文不再讨论。

 

  为了不污染用户的根命名空间,一般不会直接使用这个外部存储的根目录。

  任何应用私有的文件的应该被放置在 Context.getExternalFilesDir返回的目录下,在应用被卸载的时候,系统会清理的就是这个目录。

  另一些共享文件应该被放置在 getExternalStoragePublicDirectory(String)返回的目录中。

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

 

  KITKAT 即Android 4.4开始,如果你的应用只是需要存储一些内部数据,可以考虑使用 :

  getExternalFilesDir(String)或者getExternalCacheDir(),它们不需要获取权限。

 

  getExternalStoragePublicDirectory(String type)这个方法接收一个参数,表明目录所放的文件的类型,传入的参数是Environment类中的DIRECTORY_XXX静态变量,比如DIRECTORY_DCIM等。

  注意:传入的类型参数不能是null,返回的目录路径有可能不存在,所以必须在使用之前确认一下,比如使用File.mkdirs创建该路径。

 

  getRootDirectory()得到Android的根目录。

  isExternalStorageEmulated()设备的外存是否是用内存模拟的,是则返回true。(API Level 11)

  isExternalStorageRemovable()设备的外存是否是可以拆卸的,比如SD卡,是则返回true。(API Level 9)

 

Context API中的目录

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

  和Environment类的getExternalStoragePublicDirectory(String type)方法类似,返回包含参数指定的特定类型文件的子目录。

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

 

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

  Android 4.4之前的版本要访问的话还是要声明读写权限的,如果没有在manifest中写权限,上面两个get方法都会返回null。

 

  与上面两个方法形成对比的是下面两个方法:

  getFilesDir() 

  getCacheDir()

  这两个方法得到的是内存上的目录。

 

  这些目录都是属于应用的,当应用被卸载的时候,里面的内容都会被移除,但是不要依赖于系统的操作。

  具体内容请看最上面的网址,在插拔sd卡的时候可能要处理一些东西,我们代码没有完善,所以出现了问题。大概就是这样,这次探索对我来说,还是有收获到的,这些方法应该在以后的实践中测试,不一定对。用哪个存储空间,看具体情况决定.

以前的Android(4.1之前的版本)中,SDcard跟路径通过“/sdcard”或者“/mnt/sdcard”来表示存储卡,而在Jelly Bean系统中修改为了“/storage/sdcard0”,以后可能还会有多个SDcard的情况。

目前为了保持和之前代码的兼容,sdcard路径做了link映射。

为了使您的代码更加健壮并且能够兼容以后的Android版本和新的设备,请通过Environment.getExternalStorageDirectory().getPath()来获取sdcard路径,

如果您需要往sdcard中保存特定类型的内容,可以考虑使用Environment.getExternalStoragePublicDirectory(String type)函数,该函数可以返回特定类型的目录,目前支持如下类型:

DIRECTORY_ALARMS //警报的铃声
DIRECTORY_DCIM //相机拍摄的图片和视频保存的位置
DIRECTORY_DOWNLOADS //下载文件保存的位置
DIRECTORY_MOVIES //电影保存的位置, 比如 通过google play下载的电影
DIRECTORY_MUSIC //音乐保存的位置
DIRECTORY_NOTIFICATIONS //通知音保存的位置
DIRECTORY_PICTURES //下载的图片保存的位置
DIRECTORY_PODCASTS //用于保存podcast(博客)的音频文件
DIRECTORY_RINGTONES //保存铃声的位置

如果您的应用需要下载以上类型的文件,则可以放到上面对应的目录中去来帮助用户查找,比如最常用的就是下载文件了。如果您开发了一个浏览器,在下载文件的时候把文件下载到Download目录则方便用户以后查找该文件,当然如果你希望用户需要通过启动您的程序来查看他们下载的文件,您也可以不这么做 ^_^。

在使用这些目录保存文件的时候,需要注意一点:其他程序也有可能在使用这些目录,在保存文件前,注意判断下文件是否已经存在,不要覆盖了用户之前的数据。


Android4.1之后Android增加了多存储卡的支持,一般手机会存在内置存储卡和外置存储卡,也可能有多个外置存储卡。如何获取存储卡路径呢?

特别是各种android设备的存储器路径,是不一样的,比如T卡路径,可能是/mnt/sdcard、/mnt/extsd、/mnt/external_sd 或者/mnt/sdcard2。有时内置存储器的路径也可能是/mnt/sdcard,而host usb存储器的路径也是各种各样的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值