纯属代码分享:Android外置存储器(SD卡)工具类
/** * */package com.lurencun.android.util;import java.io.File;import java.io.IOException;import android.os.Environment;/** * @author chenyoca [桥下一粒砂] ([email protected]) * @date 2011-12-8 * @desc 外置存储器(通常是SD卡)辅助使用类 */public class ExternalStorager { private static final String STATE = Environment.getExternalStorageState(); private static final String MKDIR_MSG = "Cannot make diretory on external storager ! It might be your application have no permission to write on external storager ."; private static final String STORAGER_NOT_READY_MSG = "External storager do not ready to rw !"; public static final String TAG = "SDCardLOG"; /** * 判断外置存储器是否已经就绪。就绪状态是指外置存储器已经挂载并且系统拥有对其可读可写权限。 * @return 就绪则返回true,否则返回false。 */ public static boolean isExternalStorageReady() { return Environment.MEDIA_MOUNTED.equals(STATE); } /** * 在外置存储器中创建目录。 * @param folder 需要创建外置存储器中的目录路径 * @return 如果创建成功或者已经存在,返回目录的完整路径(路径末尾包括File.separator符号“/”)。 * @throws IOException 当外置存储器未就绪,或者写入外置存储器失败(可能是应该没有读写权限)时,抛出此异常。 */ public static String mkdir(String folder) throws IOException { String _storage_path = getExternalStoragePath(); if (null == _storage_path) throw new IOException(STORAGER_NOT_READY_MSG); StringBuffer _path = new StringBuffer(_storage_path); if(!folder.startsWith(File.separator)) _path.append(File.separator); _path.append(folder); if(!folder.endsWith(File.separator)) _path.append(File.separator); File _dir = new File(_path.toString()); if(!_dir.exists()){ if(!_dir.mkdirs())throw new IOException(MKDIR_MSG); } return _path.toString(); } /** * 获取外置存储器的路径 * @return 外置存储器已经就绪,返回其路径,否则返回null。 */ public static String getExternalStoragePath() { String _path = isExternalStorageReady() ? Environment.getExternalStorageDirectory().getPath() : null; return _path; }}