布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv_state" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true"/></RelativeLayout>
MainActivity代码
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tvState = (TextView) findViewById(R.id.tv_state); //获得sd卡的内存状态 File sdCardDir = Environment.getExternalStorageDirectory() ; String sd = getMemoryInfo(sdCardDir) ; //获得手机内部的存储状态 File dataDir = Environment.getDataDirectory() ; String phone = getMemoryInfo(dataDir) ; tvState.setText("SD卡: \n"+sd+"\n\n手机内部: \n"+phone); } 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,blockSize* totalBlocks) ; //可用空间 String availableMemory = Formatter.formatFileSize(this, blockSize*availableBlocks) ; return "总空间: "+totalMemory+"\n"+"可用空间: "+availableMemory; } }
效果图
由于测试的手机没有sdCard,所以显示sdCard的总空间和可用空间和手机内部存储的一致,若手机有sdCard就会根据sdCard的总空间和可用空间来显示了。
點擊查看更多內(nèi)容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦