获取SD卡和手机内存空间大小

	@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 + "\n手机内部: " + dataMemory);
	}

	/**
	 * 根据路径获取内存状态
	 * @param path
	 * @return
	 */
	private String getMemoryInfo(File path) {
		// 获得一个磁盘状态对象
        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, availableBlocks * blockSize);
		
		return "总空间: " + totalMemory + "\n可用空间: " + availableMemory;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
获取手机存储空间值 private String getInternalMemoryPath() { return Environment.getDataDirectory().getPath(); } /** * * @return 内置sd卡路径 */ private String getExternalMemoryPath() { // return Environment.getExternalStorageDirectory().getPath(); return "/mnt/sdcard"; } /** * * @return 外置sd卡路径 */ private String getSDCard2MemoryPath() { return "/mnt/sdcard1"; } /** * * @param path * 文件路径 * @return 文件路径的StatFs对象 * @throws Exception * 路径为空或非法异常抛出 */ private StatFs getStatFs(String path) { try { return new StatFs(path); } catch (Exception e) { e.printStackTrace(); } return null; } /** * * @param stat * 文件StatFs对象 * @return 剩余存储空间的MB数 * */ private float calculateSizeInMB(StatFs stat) { if (stat != null) return stat.getAvailableBlocks() * (stat.getBlockSize() / (1024f * 1024f)); return 0.0f; } /** * * @return ROM剩余存储空间的MB数 */ private float getAvailableInternalMemorySize() { String path = getInternalMemoryPath();// 获取数据目录 StatFs stat = getStatFs(path); return calculateSizeInMB(stat); } /** * * @return 内置SDCard剩余存储空间MB数 */ private float getAvailableExternalMemorySize() { String path = getExternalMemoryPath();// 获取数据目录 StatFs stat = getStatFs(path); return calculateSizeInMB(stat); } /** * * @return 外置SDCard剩余存储空间MB数 */ private float getAvailableSDCard2MemorySize() { // String status = Environment.getExternalStorageState(); // if (status.equals(Environment.MEDIA_MOUNTED)) { // } String path = getSDCard2MemoryPath(); // 获取数据目录 StatFs stat = getStatFs(path); return calculateSizeInMB(stat); }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值