Android Dev Notes - 26- 关于IO

检查sdcard文件的状态,是否sdcard是否可用,sdcard是否可写。


protected void checkSDcardStatus(){
		
	boolean mExternalStorageAvailable = false;
        boolean mExternalStorageWriteable = false;
        String state = Environment.getExternalStorageState();

        if (Environment.MEDIA_MOUNTED.equals(state)) {
            mExternalStorageAvailable = mExternalStorageWriteable = true;
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            mExternalStorageAvailable = true;
            mExternalStorageWriteable = false;
        } else {
            mExternalStorageAvailable = mExternalStorageWriteable = false;
        }
}

文件路径


		
                //获取sdcard根目录
                File sdcardDir = Environment.getExternalStorageDirectory();
		String path = sdcardDir.getParent() + java.io.File.separator
				+ sdcardDir.getName();
		System.out.println("sdcardDir=" + sdcardDir.getAbsolutePath());
		System.out.println("sdcardDirParent=" + sdcardDir.getParent());
		System.out.println("sdcardDirName=" + sdcardDir.getName());
		System.out.println("path=" + path);
                //【推荐使用】java.io.File.separator表示文件路径的分隔符,由java根据系统选择是\r\n,\r还是\n
                String filePath = path + java.io.File.separator + "My Documents"
				+ java.io.File.separator + "My Recordings";
		System.out.println("filepath=" + filePath);
		File myRecAudioFile = new File(filePath, "Kalimba.mp3");
                // 检查文件是否存在
                System.out.println("isExist=" + myRecAudioFile.exists());
		System.out.println("audioFileAbsPath="
				+ myRecAudioFile.getAbsolutePath());
		try {
                       //【推荐使用】带.和..的形式的文件名也解析出来
                       System.out.println("audioFileCanPath="
					+ myRecAudioFile.getCanonicalPath());
		} catch (IOException e) {
			e.printStackTrace();
		}


09-29 11:59:00.111: INFO/System.out(26594): sdcardDir=/mnt/sdcard
09-29 11:59:00.111: INFO/System.out(26594): sdcardDirParent=/mnt
09-29 11:59:00.111: INFO/System.out(26594): sdcardDirName=sdcard
09-29 11:59:00.111: INFO/System.out(26594): path=/mnt/sdcard
09-29 11:59:00.111: INFO/System.out(26594): filepath=/mnt/sdcard/My Documents/My Recordings
09-29 11:59:00.111: INFO/System.out(26594): isExist=true
09-29 11:59:00.111: INFO/System.out(26594): audioFileAbsPath=/mnt/sdcard/My Documents/My Recordings/Kalimba.mp3
09-29 11:59:00.111: INFO/System.out(26594): audioFileCanPath=/mnt/sdcard/My Documents/My Recordings/Kalimba.mp3




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值