Android中StatFs获取系统/sdcard存储(剩余空间)大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.orgcent.util;
 
import java.io.File;
 
import android.os.Environment;
import android.os.StatFs;
 
public class MemoryStatus {
     static final int ERROR = - 1 ;
 
     /**
      * 外部存储是否可用
      * @return
      */
     static public boolean externalMemoryAvailable() {
         return android.os.Environment.getExternalStorageState().equals(
                 android.os.Environment.MEDIA_MOUNTED);
     }
 
     /**
      * 获取手机内部可用空间大小
      * @return
      */
     static public long getAvailableInternalMemorySize() {
         File path = Environment.getDataDirectory();
         StatFs stat = new StatFs(path.getPath());
         long blockSize = stat.getBlockSize();
         long availableBlocks = stat.getAvailableBlocks();
         return availableBlocks * blockSize;
     }
 
     /**
      * 获取手机内部空间大小
      * @return
      */
     static public long getTotalInternalMemorySize() {
         File path = Environment.getDataDirectory();
         StatFs stat = new StatFs(path.getPath());
         long blockSize = stat.getBlockSize();
         long totalBlocks = stat.getBlockCount();
         return totalBlocks * blockSize;
     }
 
     /**
      * 获取手机外部可用空间大小
      * @return
      */
     static public long getAvailableExternalMemorySize() {
         if (externalMemoryAvailable()) {
             File path = Environment.getExternalStorageDirectory();
             StatFs stat = new StatFs(path.getPath());
             long blockSize = stat.getBlockSize();
             long availableBlocks = stat.getAvailableBlocks();
             return availableBlocks * blockSize;
         } else {
             return ERROR;
         }
     }
 
     /**
      * 获取手机外部空间大小
      * @return
      */
     static public long getTotalExternalMemorySize() {
         if (externalMemoryAvailable()) {
             File path = Environment.getExternalStorageDirectory();
             StatFs stat = new StatFs(path.getPath());
             long blockSize = stat.getBlockSize();
             long totalBlocks = stat.getBlockCount();
             return totalBlocks * blockSize;
         } else {
             return ERROR;
         }
     }
 
     static public String formatSize( long size) {
         String suffix = null ;
 
         if (size >= 1024 ) {
             suffix = "KiB" ;
             size /= 1024 ;
             if (size >= 1024 ) {
                 suffix = "MiB" ;
                 size /= 1024 ;
             }
         }
 
         StringBuilder resultBuffer = new StringBuilder(Long.toString(size));
         int commaOffset = resultBuffer.length() - 3 ;
         while (commaOffset > 0 ) {
             resultBuffer.insert(commaOffset, ',' );
             commaOffset -= 3 ;
         }
 
         if (suffix != null )
             resultBuffer.append(suffix);
         return resultBuffer.toString();
     }
}

 

在存储文件时,为了保证有充足的剩余空间大小,通常需要知道系统内部或者sdcard的存储大小。上面是一个获取存储空间的工具类。

 

结伴旅游,一个免费的交友网站:www.jieberu.com

推推族,免费得门票,游景区:www.tuituizu.com

转载于:https://www.cnblogs.com/rabbit-bunny/p/4203579.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值