Android 中的Storage state

本文详细介绍了Android系统中存储设备的不同状态及其含义,并重点强调了在进行文件读写操作时如何判断存储是否可用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

查看源码可知,android中的storage state 共有以下几种状态



/**
 * Unknown storage state, such as when a path isn't backed by known storage
 * media.
 *
 * @see #getExternalStorageState(File)
 */
public static final String MEDIA_UNKNOWN = "unknown";    未知

/**
 * Storage state if the media is not present.
 *
 * @see #getExternalStorageState(File)
 */
public static final String MEDIA_REMOVED = "removed";

/**
 * Storage state if the media is present but not mounted.
 *
 * @see #getExternalStorageState(File)
 */
public static final String MEDIA_UNMOUNTED = "unmounted";

/**
 * Storage state if the media is present and being disk-checked.
 *
 * @see #getExternalStorageState(File)
 */
public static final String MEDIA_CHECKING = "checking";

/**
 * Storage state if the media is present but is blank or is using an
 * unsupported filesystem.
 *
 * @see #getExternalStorageState(File)
 */
public static final String MEDIA_NOFS = "nofs";

/**
 * Storage state if the media is present and mounted at its mount point with
 * read/write access.
 *
 * @see #getExternalStorageState(File)
 */
public static final String MEDIA_MOUNTED = "mounted";

/**
 * Storage state if the media is present and mounted at its mount point with
 * read-only access.
 *
 * @see #getExternalStorageState(File)
 */
public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";

/**
 * Storage state if the media is present not mounted, and shared via USB
 * mass storage.
 *
 * @see #getExternalStorageState(File)
 */
public static final String MEDIA_SHARED = "shared";

/**
 * Storage state if the media was removed before it was unmounted.
 *
 * @see #getExternalStorageState(File)
 */
public static final String MEDIA_BAD_REMOVAL = "bad_removal";

/**
 * Storage state if the media is present but cannot be mounted. Typically
 * this happens if the file system on the media is corrupted.
 *
 * @see #getExternalStorageState(File)
 */
public static final String MEDIA_UNMOUNTABLE = "unmountable";

/**
 * Storage state if the media is in the process of being ejected.
 *
 * @see #getExternalStorageState(File)
 */
public static final String MEDIA_EJECTING = "ejecting";  


以上这些状态中
/**
 * Storage state if the media is present and mounted at its mount point with
 * read/write access.
 *
 * @see #getExternalStorageState(File)
 */
public static final String MEDIA_MOUNTED = "mounted";

以上的这些状态中只有mounted 状态是同时可读可写的

/**
 * Storage state if the media is present and mounted at its mount point with
 * read-only access.
 *
 * @see #getExternalStorageState(File)
 */
public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro";
mounted_ro 有读的权限,无写的权限。

结论:

因此,在文件读写的时候,通常会这么判断
if (
Environment.MEDIA_MOUNTED
.equals(
Environment.getExternalStorageState()
)){
}
else{
return null;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值