zoukankan      html  css  js  c++  java
  • 004_01获取SD容量

     1 package com.example.sdcard;
     2 
     3 import java.io.File;
     4 
     5 import android.app.Activity;
     6 import android.os.Build;
     7 import android.os.Bundle;
     8 import android.os.Environment;
     9 import android.os.StatFs;
    10 import android.text.format.Formatter;
    11 import android.view.Menu;
    12 import android.view.MenuItem;
    13 import android.widget.TextView;
    14 
    15 public class MainActivity extends Activity {
    16 
    17     private long blockSize;
    18     private long totalBlocks;
    19     private long availableBlocks;
    20 
    21     @Override
    22     protected void onCreate(Bundle savedInstanceState) {
    23         super.onCreate(savedInstanceState);
    24         setContentView(R.layout.activity_main);
    25         
    26         TextView tv_available = (TextView) findViewById(R.id.tv_available);
    27         TextView tv_total =(TextView) findViewById(R.id.tv_total);
    28         
    29         File path = Environment.getExternalStorageDirectory();
    30         StatFs stat = new StatFs(path.getPath());
    31         
    32         if(Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2){
    33             blockSize = stat.getBlockSizeLong();
    34             totalBlocks = stat.getBlockCountLong();
    35             availableBlocks = stat.getAvailableBlocksLong();
    36         }else{
    37             blockSize = stat.getBlockSize();
    38             totalBlocks = stat.getBlockCount();
    39             availableBlocks = stat.getAvailableBlocks();
    40         }
    41         
    42         //tv_available.setText(availableBlocks * blockSize + "/byte");
    43         //tv_total.setText(totalBlocks * blockSize + "/byte");
    44         
    45         String sdavail = formatSize(availableBlocks * blockSize);
    46         String sdtotal = formatSize(totalBlocks * blockSize);
    47         tv_available.setText(sdavail);
    48         tv_total.setText(sdtotal);
    49         
    50     }
    51     private String formatSize(long size){
    52         return Formatter.formatFileSize(this, size);
    53     }
    54 }
    MainActivity.java
    物随心转,境由心造,一切烦恼皆由心生。
  • 相关阅读:
    if __name__ == '__main__' 用法理解
    VSCode 使用
    sys.argv用法简介
    [Python3] RSA的加解密和签名/验签实现 -- 使用pycrytodome
    python requests 超时与重试
    collections模块之defaultdict()与namedtuple()方法简单介绍
    setdefault函数的用法及理解
    python并发编程之IO模型 同步 异步 阻塞 非阻塞
    django+uWSGI+nginx的工作原理流程与部署过程
    Nginx静态服务配置---详解root和alias指令
  • 原文地址:https://www.cnblogs.com/woodrow2015/p/4515679.html
Copyright © 2011-2022 走看看