zoukankan      html  css  js  c++  java
  • 获得手机内存空间状态

    示例

    
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            TextView tvMemoryInfo = (TextView) findViewById(R.id.tv_memory_info);
    
            //获取sd卡内存状态
            File sdCardFileDir = Environment.getExternalStorageDirectory();
            String sdcardMemory = getMemoryInfo(sdCardFileDir);
    
            //获取手机内部存储空间的状态
            File dataFileDir = Environment.getDataDirectory();
            String dataMemory = getMemoryInfo(dataFileDir);
            tvMemoryInfo.setText("sd卡" + sdcardMemory + "
    手机内部"+dataMemory);
    
        }
    
        /**
         * 根据路径获取内存状态
         * @param path
         * @return
         */
        private String getMemoryInfo(File path) {
            // 得到路径
            // File path =Environment.getExternalStorageDirectory();
    
            // 获得一个 磁盘状态 对象
            StatFs stat = new StatFs(path.getPath());
    
            long blockSize = stat.getBlockSize(); // 获得一个 扇区大小
    
            long totalBlocks = stat.getBlockCount(); // 获取扇区的总数
    
            long availableBlocks = stat.getAvailableBlocks(); // 获取可用的扇区数量
    
            // 总空间
            String totalMemory = Formatter.formatFileSize(this, totalBlocks * blockSize);
    
            // 可用空间
            String availableMemory = Formatter.formatFileSize(this, blockSize * availableBlocks);
    
            return "总空间" + totalMemory + "
    可用空间"+availableMemory;
        }
    }
  • 相关阅读:
    数据库字段包含反斜杠的查询处理
    sql中的日期时间处理
    查询时间的测试
    group by 和 聚合函数的使用
    比较版本号
    sql IIF函数的应用
    win10系统杀毒功能
    php性能的问题
    linux 下ab压力测试
    datatables的学习总结
  • 原文地址:https://www.cnblogs.com/rysinal/p/5834488.html
Copyright © 2011-2022 走看看