代码:Environment.getExternalStorageState();
范例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
String status = Environment.getExternalStorageState(); if (status.equalsIgnoreCase(Environment.MEDIA_MOUNTED)) { Toast.makeText(TechoLite.this, "SD卡存在", Toast.LENGTH_LONG).show(); // 此时SD是可读写的 } else if (status.equalsIgnoreCase(Environment.MEDIA_MOUNTED_READ_ONLY)) { Toast.makeText(TechoLite.this, "虽然SD存在,但是为只读状态", Toast.LENGTH_LONG).show(); } else if (status.equalsIgnoreCase(Environment.MEDIA_REMOVED)) { Toast.makeText(TechoLite.this, "SD不存在", Toast.LENGTH_LONG).show(); } else if (status.equalsIgnoreCase(Environment.MEDIA_SHARED)) { Toast.makeText(TechoLite.this, "虽然SD卡存在,但是正与PC等相连接", Toast.LENGTH_LONG).show(); } else if (status.equalsIgnoreCase(Environment.MEDIA_BAD_REMOVAL)) { Toast.makeText(TechoLite.this, "SD卡在挂载状态下被错误取出", Toast.LENGTH_LONG).show(); } else if (status.equalsIgnoreCase(Environment.MEDIA_CHECKING)) { Toast.makeText(TechoLite.this, "正在检查SD卡", Toast.LENGTH_LONG).show(); } else if (status.equalsIgnoreCase(Environment.MEDIA_NOFS)) { Toast.makeText(TechoLite.this, "虽然SD卡存在,但其文件系统不被支持", Toast.LENGTH_LONG).show(); } else if (status.equalsIgnoreCase(Environment.MEDIA_UNMOUNTABLE)) { Toast.makeText(TechoLite.this, "虽然SD卡存在,但是无法被挂载", Toast.LENGTH_LONG).show(); } else if (status.equalsIgnoreCase(Environment.MEDIA_UNMOUNTED)) { Toast.makeText(TechoLite.this, "虽然SD卡存在,但是未被挂载", Toast.LENGTH_LONG).show(); } else { Toast.makeText(TechoLite.this, "其他原因", Toast.LENGTH_LONG).show(); } |
说明:用于判断设备的SD卡的状态。