Android SDCard操作(文件读写,容量计算)
http://crackren.iteye.com/blog/747121
/*打开文件输出流,名称txtME,以不覆盖模式打开*/
//打开一个文件输入流。名称为txtME
FileOutputStream os =FileReadWriterActivity.this.openFileOutput("txtME",MODE_PRIVATE);
FileInputStream in = FileReadWriterActivity.this.openFileInput("txtME");
android.os.Environment
提供访问环境变量
java.lang.Object
android.os.Environment
Environment 静态方法:
方法 : getDataDirectory ()
返回 : File
解释 : 返回Data的目录
方法 : getDownloadCacheDirectory ()
返回 : File
解释 : 返回下载缓冲区目录
方法 : getExternalStorageDirectory ()
返回 : File
解释 : 返回扩展存储区目录(SDCard)
方法 : getExternalStoragePublicDirectory (String type)
返回 : File
解释 : 返回一个高端的公用的外部存储器目录来摆放某些类型的文件(来自网上)
方法 : getRootDirectory ()
返回 : File
解释 : 返回Android的根目录
方法 : getExternalStorageState ()
返回 : String
解释 : 返回外部存储设备的当前状态
getExternalStorageState () 返回的状态String 类型常量 :
常量 : MEDIA_BAD_REMOVAL
值 : "bad_removal"
解释 : 在没有正确卸载SDCard之前移除了
常量 : MEDIA_CHECKING
值 : "checking"
解释 : 正在磁盘检查
常量 : MEDIA_MOUNTED
值 : "mounted"
解释 : 已经挂载并且拥有可读可写权限
常量 : MEDIA_MOUNTED_READ_ONLY
值 : "mounted_ro"
解释 : 已经挂载,但只拥有可读权限
常量 : MEDIA_NOFS
值 : "nofs"
解释 : 对象空白,或者文件系统不支持
常量 : MEDIA_REMOVED
值 : "removed"
解释 : 已经移除扩展设备
常量 : MEDIA_SHARED
值 : "shared"
解释 : 如果SDCard未挂载,并通过USB大容量存储共享
常量 : MEDIA_UNMOUNTABLE
值 : "unmountable"
解释 : 不可以挂载任何扩展设备
常量 : MEDIA_UNMOUNTED
值 : "unmounted"
解释 : 已经卸载
使用时只需先判断SDCard当前的状态然后取得SdCard的目录即可(见源代码)
Java代码
Java代码
//SDcard 操作 public void SDCardTest() { // 获取扩展SD卡设备状态 String sDStateString = android.os.Environment.getExternalStorageState(); // 拥有可读可写权限 if (sDStateString.equals(android.os.Environment.MEDIA_MOUNTED)) { try { // 获取扩展存储设备的文件目录 File SDFile = android.os.Environment .getExternalStorageDirectory(); // 打开文件 File myFile = new File(SDFile.getAbsolutePath() + File.separator + "MyFile.txt"); // 判断是否存在,不存在则创建 if (!myFile.exists()) { myFile.createNewFile(); } // 写数据 String szOutText = "Hello, World!"; FileOutputStream outputStream = new FileOutputStream(myFile); outputStream.write(szOutText.getBytes()); outputStream.close(); } catch (Exception e) { // TODO: handle exception }// end of try }// end of if(MEDIA_MOUNTED) // 拥有只读权限 else if (sDStateString .endsWith(android.os.Environment.MEDIA_MOUNTED_READ_ONLY)) { // 获取扩展存储设备的文件目录 File SDFile = android.os.Environment.getExternalStorageDirectory(); // 创建一个文件 File myFile = new File(SDFile.getAbsolutePath() + File.separator + "MyFile.txt"); // 判断文件是否存在 if (myFile.exists()) { try { // 读数据 FileInputStream inputStream = new FileInputStream(myFile); byte[] buffer = new byte[1024]; inputStream.read(buffer); inputStream.close(); } catch (Exception e) { // TODO: handle exception }// end of try }// end of if(myFile) }// end of if(MEDIA_MOUNTED_READ_ONLY) // end of func
public void SDCardSizeTest() { // 取得SDCard当前的状态 String sDcString = android.os.Environment.getExternalStorageState(); if (sDcString.equals(android.os.Environment.MEDIA_MOUNTED)) { // 取得sdcard文件路径 File pathFile = android.os.Environment .getExternalStorageDirectory(); android.os.StatFs statfs = new android.os.StatFs(pathFile.getPath()); // 获取SDCard上BLOCK总数 long nTotalBlocks = statfs.getBlockCount(); // 获取SDCard上每个block的SIZE long nBlocSize = statfs.getBlockSize(); // 获取可供程序使用的Block的数量 long nAvailaBlock = statfs.getAvailableBlocks(); // 获取剩下的所有Block的数量(包括预留的一般程序无法使用的块) long nFreeBlock = statfs.getFreeBlocks(); // 计算SDCard 总容量大小MB long nSDTotalSize = nTotalBlocks * nBlocSize / 1024 / 1024; // 计算 SDCard 剩余大小MB long nSDFreeSize = nAvailaBlock * nBlocSize / 1024 / 1024; }// end of if // end of func