`

android获取设备存储信息

阅读更多
这是获取android设备的存储设备(手机内存,SD卡内存)信息的代码
import java.io.File;   

import android.os.Environment;   
import android.os.StatFs;   
  
public class MemoryStatus {   
  
    static final int ERROR = -1;   
       
    static public boolean externalMemoryAvailable() {   
        return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);   
    }   
       /**
        * 获取可用的内部记忆大小
        * @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;   
    }   
       /**
        * 获取总共内部内存大小
        * @return
        */
    static public long getTotalInternalMemorySize() {   
        File path = Environment.getDataDirectory();   
        StatFs stat = new StatFs(path.getPath());   
        long blockSize = stat.getBlockSize();   
        long totalBlocks = stat.getBlockCount();   
        return totalBlocks * blockSize;   
    }   
       
    /**
     * 获取可用外部记忆大小
     * @return
     */
    static public long getAvailableExternalMemorySize() {   
        if(externalMemoryAvailable()) {   
            File path = Environment.getExternalStorageDirectory();   
            StatFs stat = new StatFs(path.getPath());   
            long blockSize = stat.getBlockSize();   
            long availableBlocks = stat.getAvailableBlocks();   
            return availableBlocks * blockSize;   
        } else {   
            return ERROR;   
        }   
    }   
       
    /**
     * 获取总共外部记忆大小
     * @return
     */
    static public long getTotalExternalMemorySize() {   
        if(externalMemoryAvailable()) {   
            File path = Environment.getExternalStorageDirectory();   
            StatFs stat = new StatFs(path.getPath());   
            long blockSize = stat.getBlockSize();   
            long totalBlocks = stat.getBlockCount();   
            return totalBlocks * blockSize;   
        } else {   
            return ERROR;   
        }   
    }   
       
    static public String formatSize(long size) {   
        String suffix = null;   
       
        if (size >= 1024) {   
            suffix = "KiB";   
            size /= 1024;   
            if (size >= 1024) {   
                suffix = "MiB";   
                size /= 1024;   
            }   
        }   
       
        StringBuilder resultBuffer = new StringBuilder(Long.toString(size));   
       
        int commaOffset = resultBuffer.length() - 3;   
        while (commaOffset > 0) {   
            resultBuffer.insert(commaOffset, ',');   
            commaOffset -= 3;   
        }   
       
        if (suffix != null)   
            resultBuffer.append(suffix);   
        return resultBuffer.toString();   
    }   
}  
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics