1. 在Manifest文件中加入权限
(1) 在sdcard中创建与删除文件的权限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
(2)在sdcard中写入文件的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2. Enviroment类的使用
(1)获取sdcard目录
public static File getExternalStorageDirectory ()
注意:返回的是文件File类型,不是String。
(2)判断当前sdcard状态
public static String getExternalStorageState ()
返回值:
MEDIA_BAD_REMOVAL:if the media was removed before it was unmounted.
MEDIA_CHECKING:f the media is present and being disk-checked
MEDIA_MOUNTED:if the media is present and mounted at its mount point with read/write access.
MEDIA_MOUNTED_READ_ONLY:if the media is present and mounted at its mount point with read only access.
MEDIA_NOFS:if the media is present but is blank or is using an unsupported filesystem
MEDIA_REMOVED:if the media is not present.
MEDIA_SHARED:if the media is present not mounted, and shared via USB mass storage.
MEDIA_UNMOUNTABLE:if the media is present but cannot be mounted.
MEDIA_UNMOUNTED:if the media is present but not mounted.
注意:在使用sdcard之前一定要使用getExternalStorageState ()判断sdcard的状态,只有在MEDIA_MOUNTED的时候,sdcard才是可读写的。