android 获取sdcard 和内存使用情况

package com.hairun.sdcard.meomery;


import java.io.File;


import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
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;


public class MainActivity extends Activity {


@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 得到文件系统的信息:存储块大小,总的存储块数量,可用存储块数量
// 获取sd卡空间
// 存储设备会被分为若干个区块
// 每个区块的大小 * 区块总数 = 存储设备的总大小
// 每个区块的大小 * 可用区块的数量 = 存储设备可用大小
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize;
long totalBlocks;
long availableBlocks;
// 由于API18(Android4.3)以后getBlockSize过时并且改为了getBlockSizeLong
// 因此这里需要根据版本号来使用那一套API
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
{
blockSize = stat.getBlockSizeLong();
totalBlocks = stat.getBlockCountLong();
availableBlocks = stat.getAvailableBlocksLong();
}
else
{
blockSize = stat.getBlockSize();
totalBlocks = stat.getBlockCount();
availableBlocks = stat.getAvailableBlocks();
}
// 利用formatSize函数把字节转换为用户等看懂的大小数值单位
TextView tv_total = (TextView) findViewById(R.id.tv_total);
TextView tv_available = (TextView) findViewById(R.id.tv_available);
String totalText = formatSize(blockSize * totalBlocks);
String availableText = formatSize(blockSize * availableBlocks);
tv_total.setText("SDCard总大小:\n" + totalText);
tv_available.setText("SDCard可用空间大小:\n" + availableText);

TextView tv_totalMomery = (TextView) findViewById(R.id.tv_total_meomery);
TextView tv_useMomery = (TextView) findViewById(R.id.tv_use_meomery);
long totalMemoery =MomeryInfo.getmem_TOLAL();
long userMemoery = MomeryInfo.getmem_UNUSED(MainActivity.this);
tv_totalMomery.setText("总的内存大小:\n"+formatSize(totalMemoery));
tv_useMomery.setText("可用内存大小:\n"+formatSize(userMemoery));
TextView tv= (TextView) findViewById(R.id.tv);
tv.setText("手机内部总的存储空间:\n"+formatSize(getTotalInternalMemorySize())+
"手机内部剩余存储空间:\n"+formatSize(getAvailableInternalMemorySize()));


}
//封装Formatter.formatFileSize方法,具体可以参考安卓的API
private String formatSize(long size)
{
return Formatter.formatFileSize(this, size);
}


/**
    * 获取手机内部剩余存储空间
    * @return
    */
   public static long getAvailableInternalMemorySize() {
       File path = Environment.getDataDirectory();
       StatFs stat = new StatFs(path.getPath());
       long blockSize = stat.getBlockSize();
       long availableBlocks = stat.getAvailableBlocks();
       return availableBlocks * blockSize;
   }


   /**
    * 获取手机内部总的存储空间
    * @return
    */
   public static long getTotalInternalMemorySize() {
       File path = Environment.getDataDirectory();
       StatFs stat = new StatFs(path.getPath());
       long blockSize = stat.getBlockSize();
       long totalBlocks = stat.getBlockCount();
       return totalBlocks * blockSize;
   }

}



package com.hairun.sdcard.meomery;


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


import android.app.ActivityManager;
import android.content.Context;


public class MomeryInfo {
 // 获得可用的内存
    public static long getmem_UNUSED(Context mContext) {
        long MEM_UNUSED;
// 得到ActivityManager
        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
// 创建ActivityManager.MemoryInfo对象  


        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
        am.getMemoryInfo(mi);


// 取得剩余的内存空间 


        MEM_UNUSED = mi.availMem / 1024;
        return MEM_UNUSED;
    }


    // 获得总内存
    public static long getmem_TOLAL() {
        long mTotal;
        // /proc/meminfo读出的内核信息进行解释
        String path = "/proc/meminfo";
        String content = null;
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(path), 8);
            String line;
            if ((line = br.readLine()) != null) {
                content = line;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // beginIndex
        int begin = content.indexOf(':');
        // endIndex
        int end = content.indexOf('k');
        // 截取字符串信息


content = content.substring(begin + 1, end).trim();
        mTotal = Integer.parseInt(content);
        return mTotal;
    }


}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值