android中的StatFs类
StatFs 一个模拟linux的df命令的一个类,获得SD卡和手机内存的使用情况 1. getAvailableBlocks() 2. 解释:返回 Int ,获取当前可用的存储空间 3. getBlockCount() 4. 解释:返回 Int ,获取该区域可用的文件系统数 5. getBlockSize() 6. 解释:返回 Int ,大小,以字节为单位,一个文件系统 7. getFreeBlocks() 8. 解释:返回 Int ,该块区域剩余的空间 9. restat(String path) 10. 解释:执行一个由该对象所引用的文件系统 复制代码 完整例子读取 SDCard 内存 1. package com.terry; 2. 3. import java.io.File; 4. import java.text.DecimalFormat; 5. import android.R.integer; 6. import android.app.Activity; 7. import android.os.Bundle; 8. import android.os.Environment; 9. import android.os.StatFs; 10. import android.view.View; 11. import android.view.View.OnClickListener; 12. import android.widget.Button; 13. import android.widget.ProgressBar; 14. import android.widget.TextView; 15. import android.widget.Toast; 16. 17. public class getStorageActivity extends Activity { 18. 19. private Button myButton; 20. 21. /** Called when the activity is first created. */ 22. 23. @Override 24. public void onCreate(Bundle savedInstanceState) { 25. 26. super.onCreate(savedInstanceState); 27. 28. setContentView(R.layout.main); 29. 30. findView(); 31. 32. viewHolder.myButton.setOnClickListener(new OnClickListener() { 33. 34. @Override 35. public void onClick(View arg0) { 36. 37. // TODO Auto-generated method stub 38. 39. getSize(); 40. 41. } 42. 43. }); 44. 45. } 46. 47. void findView(){ 48. 49. viewHolder.myButton=(Button)findViewById(R.id.Button01); 50. 51. viewHolder.myBar=(ProgressBar)findViewById(R.id.myProgressBar); 52. 53. viewHolder.myTextView=(TextView)findViewById(R.id.myTextView); 54. 55. } 56. 57. void getSize(){ 58. 59. viewHolder.myTextView.setText(""); 60. 61. viewHolder.myBar.setProgress(0); 62. 63. //判断是否有插入存储卡 64. 65. if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ 66. 67. File path =Environment.getExternalStorageDirectory(); 68. 69. //取得sdcard文件路径 70. 71. StatFs statfs=new StatFs(path.getPath()); 72. 73. //获取block的SIZE 74. 75. long blocSize=statfs.getBlockSize(); 76. 77. //获取BLOCK数量 78. 79. long totalBlocks=statfs.getBlockCount(); 80. 81. //己使用的Block的数量 82. 83. long availaBlock=statfs.getAvailableBlocks(); 84. 85. String[] total=filesize(totalBlocks*blocSize); 86. 87. String[] availale=filesize(availaBlock*blocSize); 88. 89. //设置进度条的最大值 90. 91. int maxValue=Integer.parseInt(availale[0]) 92. 93. *viewHolder.myBar.getMax()/Integer.parseInt(total[0]); 94. 95. viewHolder.myBar.setProgress(maxValue); 96. 97. String Text="总共:"+total[0]+total[1]+"\n"+"可用:"+availale[0]+availale[1]; 98. 99. viewHolder.myTextView.setText(Text); 100. 101. } 102. 103. else if(Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED)){ 104. 105. Toast.makeText(getStorageActivity.this, "没有sdCard", 1000).show(); 106. 107. } 108. 109. } 110. 111. //返回数组,下标1代表大小,下标2代表单位 KB/MB 112. 113. String[] filesize(long size){ 114. 115. String str=""; 116. 117. if(size>=1024){ 118. 119. str="KB"; 120. 121. size/=1024; 122. 123. if(size>=1024){ 124. 125. str="MB"; 126. 127. size/=1024; 128. 129. } 130. 131. } 132. 133. DecimalFormat formatter=new DecimalFormat(); 134. 135. formatter.setGroupingSize(3); 136. 137. String result[] =new String[2]; 138. 139. result[0]=formatter.format(size); 140. 141. result[1]=str; 142. 143. return result; 144. 145. } 146. 147. } 复制代码 |
package idv.nightgospel.information;
import java.io.File;
import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;
public class SDCardUsage {
privatestatic File path = Environment.getExternalStorageDirectory();
privatestatic StatFs stat = new StatFs(path.getPath());
privatestatic long blockSize = stat.getBlockSize();
privatestatic long totalBlocks = stat.getBlockCount();
privatestatic long availableBlocks;
publicstatic String getTotalStorage(Context context){
stat.getAvailableBlocks();
returnFormatter.formatFileSize(context, Long.parseLong(totalBlocks * blockSize +""));
}
publicstatic String getFreeStorage(Context context){
returnFormatter.formatFileSize(context, Long.parseLong(stat.getAvailableBlocks()*blockSize + ""));
}
publicstatic boolean isCardAvailable(){
if(Environment.getExternalStorageState().equals(Environment.MEDIA_UNMOUNTED)
||Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED)
||Environment.getExternalStorageState().equals(Environment.MEDIA_BAD_REMOVAL)
||Environment.getExternalStorageState().equals(Environment.MEDIA_UNMOUNTABLE)
||Environment.getExternalStorageState().equals(Environment.MEDIA_NOFS))
returnfalse;
else
returntrue;
}
}