Android:StatFs类 获取系统/sdcard存储空间信息

在存储文件时,为了保证有充足的存储空间大小,通常需要知道系统内部或者sdcard的剩余存储空间大小,这里就需要用到StatFs类。

1. 判断 SDCard 是否存在,并且是否具有可读写权限 

/**

* 外部存储是否可用 (存在且具有读写权限)

* @return

*/ 

staticpublicboolean isExternalStorageAvailable() { 

    return Environment.getExternalStorageState().equals( 

            Environment.MEDIA_MOUNTED); 

     /**
     * 外部存储是否可用 (存在且具有读写权限)
     * @return
     */
    static public boolean isExternalStorageAvailable() {
        return Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
    }

2.获取手机系统可用空间大小

 

/**

* 获取手机内部可用空间大小

* @return

*/ 

staticpubliclong getAvailableInternalMemorySize() { 

    File path = Environment.getDataDirectory(); 

    StatFs stat = new StatFs(path.getPath()); 

    long blockSize = stat.getBlockSize(); 

    long availableBlocks = stat.getAvailableBlocks(); 

    return availableBlocks * blockSize; 

    /**
     * 获取手机内部可用空间大小
     * @return
     */
    static public long getAvailableInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return availableBlocks * blockSize;
    }

 

 

3.获取手机内部总空间大小

/**

* 获取手机内部空间大小

* @return

*/ 

staticpubliclong getTotalInternalMemorySize() { 

    File path = Environment.getDataDirectory();//Gets the Android data directory 

    StatFs stat = new StatFs(path.getPath()); 

    long blockSize = stat.getBlockSize();      //每个block 占字节数 

    long totalBlocks = stat.getBlockCount();   //block总数 

    return totalBlocks * blockSize; 

    /**
     * 获取手机内部空间大小
     * @return
     */
    static public long getTotalInternalMemorySize() {
        File path = Environment.getDataDirectory();//Gets the Android data directory
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();      //每个block 占字节数
        long totalBlocks = stat.getBlockCount();   //block总数
        return totalBlocks * blockSize;
    }

4.获取手机外部可用空间大小、获取手机外部总空间大小

/**

    * 获取手机外部可用空间大小

    * @return

    */ 

   staticpubliclong getAvailableExternalMemorySize() { 

       if (isExternalStorageAvailable()) { 

           File path = Environment.getExternalStorageDirectory();//获取SDCard根目录 

           StatFs stat = new StatFs(path.getPath()); 

           long blockSize = stat.getBlockSize(); 

           long availableBlocks = stat.getAvailableBlocks(); 

           return availableBlocks * blockSize; 

       } else

           return -1

       } 

   } 

 

   /**

    * 获取手机外部总空间大小

    * @return

    */ 

   staticpubliclong getTotalExternalMemorySize() { 

       if (isExternalStorageAvailable()) { 

           File path = Environment.getExternalStorageDirectory(); //获取SDCard根目录 

           StatFs stat = new StatFs(path.getPath()); 

           long blockSize = stat.getBlockSize(); 

           long totalBlocks = stat.getBlockCount(); 

           return totalBlocks * blockSize; 

       } else

           return -1

       } 

   } 

 /**
     * 获取手机外部可用空间大小
     * @return
     */
    static public long getAvailableExternalMemorySize() {
        if (isExternalStorageAvailable()) {
            File path = Environment.getExternalStorageDirectory();//获取SDCard根目录
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long availableBlocks = stat.getAvailableBlocks();
            return availableBlocks * blockSize;
        } else {
            return -1;
        }
    }

    /**
     * 获取手机外部总空间大小
     * @return
     */
    static public long getTotalExternalMemorySize() {
        if (isExternalStorageAvailable()) {
            File path = Environment.getExternalStorageDirectory(); //获取SDCard根目录
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long totalBlocks = stat.getBlockCount();
            return totalBlocks * blockSize;
        } else {
            return -1;
        }
    }

 

 

注意: 访问SDCard进行文件读写时:

 

在AndroidManifest.xml中加入访问SDCard的权限如下:

<!-- 在SDCard中创建与删除文件权限 -->

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

<!-- 往SDCard写入数据权限 -->

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

 

接着我们在使用SDcard进行读写的时候 会用到Environment类下面的几个静态方法:

1:getDataDirectory()         获取到Android中的data数据目录

2:getDownloadCacheDirectory() 获取到下载的缓存目录

3:getExternalStorageDirectory()     获取到外部存储的目录 一般指SDcard

4:getExternalStorageState()           获取外部设置的当前状态 一般指SDcard,

android系统中对于外部设置的状态,比较常用的是 MEDIA_MOUNTED(SDcard存在且可以进行读写) MEDIA_MOUNTED_READ_ONLY (SDcard存在,只可以进行读操作) 当然还有其他的一些状态,可以在文档中进行查找到。

5:getRootDirectory()                   获取到Android Root路径

6:isExternalStorageEmulated()    返回Boolean值判断外部设置是否有效

7:isExternalStorageRemovable() 返回Boolean值,判断外部设置是否可以移除

posted @ 2014-03-10 23:24  欢醉  阅读(14537)  评论(1编辑  收藏  举报