android getExternalStorageDirectory() 和 getExternalStorageState()

更详细的内容Android 文件读写以及assets操作


---------------------------------------------------------------------------------------------------------------------------------------------


参考:《第一行代码》第8章

http://blog.csdn.net/yuzhiboyi/article/details/8645730

#####################################################################


/**
     * 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
     * {@link #getExternalStorageState()}.
     * <p>
     * <em>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.</em>
     * <p>
     * On devices with multiple users (as described by {@link UserManager}),
     * each user has their own isolated external storage. Applications only have
     * access to the external storage for the user they're running as.
     * <p>
     * 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
     * <p>
     * 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
     * {@link android.content.Context#getExternalFilesDir
     * 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
     * {@link #getExternalStoragePublicDirectory}.
     * <p>
     * Writing to this path requires the
     * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission,
     * and starting in read access requires the
     * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission,
     * which is automatically granted if you hold the write permission.
     * <p>
     * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your
     * application only needs to store internal data, consider using
     * {@link Context#getExternalFilesDir(String)} or
     * {@link Context#getExternalCacheDir()}, which require no permissions to
     * read or write.
     * <p>
     * This path may change between platform versions, so applications should
     * only persist relative paths.
     * <p>
     * Here is an example of typical code to monitor the state of external
     * storage:
     * <p>
     * {@sample
     * development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java
     * monitor_storage}
     *
     * @see #getExternalStorageState()
     * @see #isExternalStorageRemovable()
     */
    public static File getExternalStorageDirectory() {
        throwIfUserRequired();
        return sCurrentUser.getExternalDirsForApp()[0];
    }


翻译:

返回主外部存储目录。如果用户在电脑上安装了此目录,或者已经从该设备上移除,又或者发生了其他一些问题,那么此目录可能无法访问,你可以通过函数

getExternalStorageState()

确定它的状态。

注意:不要被“外部”这个词所迷惑。可能用媒体存储或者共享存储能更好的理解此目录。它是一个文件系统,拥有一个比较大的数据量,并且被所有应用程序所共享(不需要权限)。通常它是一个SD卡,不过也可以用设备上的内置存储实现(此内置存储和受保护的内部存储严格区分开),并且可以作为一个文件系统安装在电脑上。

一台设备上可以有多个用户,并且每个用户拥有自己独立的外部存储,应用程序只能访问正在运行用户的外部存储

在有多个外部存储路径的设备上,此目录表示主外部存储目录(用户将会和它进行互动)。不过二级外部存储也能够访问

为了避免污染用户的根命名系统,不应该直接使用顶层目录。

应用程序私人的文件应该被放置在某个目录,该目录可以通过

@link android.content.Context#getExternalFilesDir
     * Context.getExternalFilesDir

得到,如果应用程序被卸载,那么系统自动会删除这些文件。其他的共享文件应该被放置在函数

@link #getExternalStoragePublicDirectory

返回的目录中

往路径上写东西需要权限

@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE

从路径例读东西需要权限

@link android.Manifest.permission#READ_EXTERNAL_STORAGE
如果你已经设置了写权限,那么读权限会被自动授与。

如果你的应用仅需要存储内部数据,考虑使用函数

@link Context#getExternalFilesDir(String)    or   <span style="font-family: Arial, Helvetica, sans-serif;">{@link Context#getExternalCacheDir()</span>

使用它们不需要读或者写的权限

路径可能会在不同平台版本之间改变,所以应该坚持使用相对路径



########################################################################


    /**
     * Returns the current state of the primary "external" storage device.
     * 
     * @see #getExternalStorageDirectory()
     * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED},
     *         {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING},
     *         {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED},
     *         {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED},
     *         {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}.
     */
    public static String getExternalStorageState() {
        final File externalDir = sCurrentUser.getExternalDirsForApp()[0];
        return getExternalStorageState(externalDir);
    }

翻译:

返回主外部存储设备的当前状态

当外部存储设备状态是MEDIA_MOUNTED时,才可读可写


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值