zoukankan      html  css  js  c++  java
  • android获取存储卡使用情况

     1 package com.aib.com;
     2 
     3 import java.io.File;
     4 import android.app.Activity;
     5 import android.os.Bundle;
     6 import android.os.Environment;
     7 import android.os.StatFs;
     8 import android.text.format.Formatter;
     9 import android.util.Log;
    10 import android.view.Menu;
    11 import android.widget.TextView;
    12 
    13 public class MainActivity extends Activity {
    14 
    15     @Override
    16     protected void onCreate(Bundle savedInstanceState) {
    17         super.onCreate(savedInstanceState);
    18         setContentView(R.layout.activity_main);
    19         getSDcardMemory();
    20     }
    21 
    22     public void getSDcardMemory() {
    23         /** 获取存储卡路径 */
    24          File sdcardDir = Environment.getExternalStorageDirectory();
    25         /** 获取手机内存路径 */
    26     //    File Dir = Environment.getDataDirectory();
    27         /** StatFs 看文件系统空间使用情况 */
    28         StatFs statFs = new StatFs(sdcardDir.getPath());
    29         /** Block 的 size */
    30         int blockSize = statFs.getBlockSize();
    31         /** 总 Block 数量 */
    32         int totalBlocks = statFs.getBlockCount();
    33         /** 可用的 Block 数量 */
    34         int availableBlocks = statFs.getAvailableBlocks();
    35         /**
    36          * android.text.format.Formatter :Utility class to aid in formatting
    37          * common values that are not covered by the standard
    38          * java.util.Formatter.
    39          */
    40         Log.i("aib", blockSize +"---"+totalBlocks+"----"+availableBlocks);
    41         String totaoSize = Formatter.formatFileSize(MainActivity.this,
    42                 totalBlocks * blockSize);
    43         String availSize = Formatter.formatFileSize(MainActivity.this,
    44                 availableBlocks * blockSize);
    45         TextView tv = (TextView) findViewById(R.id.tv);
    46         tv.setText(sdcardDir.getPath()+ "
    总空间: "
    47                 + totaoSize +"
    可用空间:"+ availSize);
    48     }
    49 
    50     @Override
    51     public boolean onCreateOptionsMenu(Menu menu) {
    52         // Inflate the menu; this adds items to the action bar if it is present.
    53         getMenuInflater().inflate(R.menu.main, menu);
    54         return true;
    55     }
    56 
    57 }
  • 相关阅读:
    JS中声明变量的细节问题
    你不知道的var! 细节
    读书笔记:对象的属性
    手写new操作符
    slice
    全相等函数 isEqual
    几个面试题
    全相等函数
    剑指 Offer 29. 顺时针打印矩阵
    剑指 Offer 28. 对称的二叉树
  • 原文地址:https://www.cnblogs.com/lolita/p/3333999.html
Copyright © 2011-2022 走看看