首页 技术 正文
技术 2022年11月23日
0 收藏 595 点赞 4,194 浏览 2284 个字
package com.example.getMem;import java.io.File;import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;
import android.widget.TextView;
import android.app.Activity;
import android.app.AlertDialog.Builder;public class MainActivity extends Activity {    private TextView tv_totalSize;
    private TextView tv_availabelSize;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        tv_totalSize=(TextView) findViewById(R.id.totalSize);
        tv_availabelSize=(TextView) findViewById(R.id.availabel);
        
        // 获取存储空间
        File dataFileDir=Environment.getDataDirectory();
        String data =getMemoryInfo(dataFileDir);
        String []d= data.split("##");
        
        tv_totalSize.setText("内部存储总大小:" + d[0]);
        tv_availabelSize.setText("内部存储可用空间是:" + d[1]);
        
        //获取Sd 卡空间
        
//        File SdFileDir=Environment.getExternalStorageDirectory();
//        //判断Sd卡是否可用
//        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//            String sdMemory =getMemoryInfo(SdFileDir);
//            String []sd=sdMemory.split("##");
//            tv_totalSize.setText("内部存储总大小:" + sd[0]);
//            tv_availabelSize.setText("内部存储可用空间是:" + sd[1]);
//            
//        }
        
    }
    private String getMemoryInfo(File path) {
        long blockSize;
        long totalBlockCount;
        long avaiLabelCount;
        // TODO Auto-generated method stub
        StatFs stat = new StatFs(path.getPath());
        
        //检测系统版本
        if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.JELLY_BEAN_MR2){
        
        //获取每个扇区的大小
         blockSize = stat.getBlockSizeLong();        
        
        //获取总共有多少扇区
         totalBlockCount=stat.getBlockCountLong();
        
        //获取可用扇区数量
         avaiLabelCount=stat.getAvailableBlocksLong();
         }else{
             blockSize=stat.getBlockSize();
             totalBlockCount=stat.getBlockCount();
             avaiLabelCount=stat.getAvailableBlocks();
            
         }
        
                
        // 磁盘总大小
        String totalMemory =Formatter.formatFileSize(this, blockSize*totalBlockCount);
        // 可用大小
        String availabelMemory=Formatter.formatFileSize(this, blockSize*avaiLabelCount);
        
        return totalMemory+"##"+availabelMemory;
    }    
    }

http://pan.baidu.com/s/1bp0V87d

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,913
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,438
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,252
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,063
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,695
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,733