android文件夹的管理

来源:互联网 发布:单片机好学吗 编辑:程序博客网 时间:2024/05/16 08:54

1、创建文件夹

File sdcardPath = Environment.getExternalStorageDirectory();String folder = sdcardPath.getAbsolutePath() + File.separator+ "文件夹名" + File.separator + "文件夹下子文件夹名";File destDir = new File(folder);if (!destDir.exists()) {<span style="white-space:pre"></span>destDir.mkdirs();}


在创建文件夹之前最好先判断一下SD卡是否存在(下面会介绍方法)



2、Android 判断SD卡是否存在及容量查询的简单方法如下:

首先要在AndroidManifest.xml中增加SD卡访问权限

<!-- 在SDCard中创建与删除文件权限 -->  <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/><!-- 往SDCard写入数据权限 --><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>   

1)SD卡是否存在

private boolean ExistSDCard() {if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {return true;} else {return false;}}

2)SD卡剩余空间

public long getSDFreeSize() {// 取得SD卡文件路径File path = Environment.getExternalStorageDirectory();StatFs sf = new StatFs(path.getPath());// 获取单个数据块的大小(Byte)long blockSize = sf.getBlockSize();// 空闲的数据块的数量long freeBlocks = sf.getAvailableBlocks();// 返回SD卡空闲大小// return freeBlocks * blockSize; //单位Byte// return (freeBlocks * blockSize)/1024; //单位KBreturn (freeBlocks * blockSize) / 1024 / 1024; // 单位MB}

3)SD卡总容量

public long getSDAllSize() {// 取得SD卡文件路径File path = Environment.getExternalStorageDirectory();StatFs sf = new StatFs(path.getPath());// 获取单个数据块的大小(Byte)long blockSize = sf.getBlockSize();// 获取所有数据块数long allBlocks = sf.getBlockCount();// 返回SD卡大小// return allBlocks * blockSize; //单位Byte// return (allBlocks * blockSize)/1024; //单位KBreturn (allBlocks * blockSize) / 1024 / 1024; // 单位MB}

3、删除文件夹及文件夹下的子文件(递归方法)

public static void DeleteFile(File file) {if (file.isFile()) {file.delete();return;}if (file.isDirectory()) {File[] childFile = file.listFiles();if (childFile == null || childFile.length == 0) {file.delete();return;}for (File f : childFile) {DeleteFile(f);}file.delete();}}



0 0
原创粉丝点击