SDCard的使用

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才是可读写的。

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值