/*
* Copyright (C) 2013 Peng fei Pan <sky@xiaopan.me>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.lazylibrary.util;
import android.os.Environment;
import java.io.File;
/**
* SD卡工具箱
*/
public class SDCardUtils {
/**
* 获取SD卡的状态
*/
public static String getState() {
return Environment.getExternalStorageState();
}
/**
* SD卡是否可用
*
* @return 只有当SD卡已经安装并且准备好了才返回true
*/
public static boolean isAvailable() {
return getState().equals(Environment.MEDIA_MOUNTED);
}
/**
* 获取SD卡的根目录
*
* @return null:不存在SD卡
*/
public static File getRootDirectory() {
return isAvailable() ? Environment.getExternalStorageDirectory() : null;
}
/**
* 获取SD卡的根路径
*
* @return null:不存在SD卡
*/
public static String getRootPath() {
File rootDirectory = getRootDirectory();
return rootDirectory != null ? rootDirectory.getPath() : null;
}
/**
*获取sd卡路径
* @return Stringpath
*/
public static String getSDPath(){
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState()
.equals(Environment.MEDIA_MOUNTED); //判断sd卡是否存在
if (sdCardExist)
{
sdDir = Environment.getExternalStorageDirectory();//获取跟目录
}
return sdDir.toString();
}
}