这个非常简单,直接上代码:
MainActivity:
package com.home.getsdinfo; import java.io.File; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.os.StatFs; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private Button getAllBtn; private Button getRemainBtn; private TextView allText; private TextView remainText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initWidget(); } private void initWidget() { getAllBtn = (Button) findViewById(R.id.main_btn_all); getAllBtn.setOnClickListener(this); getRemainBtn = (Button) findViewById(R.id.main_btn_remain); getRemainBtn.setOnClickListener(this); allText = (TextView) findViewById(R.id.main_tv_all); remainText = (TextView) findViewById(R.id.main_tv_remain); } @Override public void onClick(View v) { if (v == getAllBtn) { if (!isExistSDCard()) { Toast.makeText(this, "SD卡不存在!", Toast.LENGTH_SHORT).show(); return; } allText.setText(getSDAllSize() + "MB"); } else if (v == getRemainBtn) { if (!isExistSDCard()) { Toast.makeText(this, "SD卡不存在!", Toast.LENGTH_SHORT).show(); return; } remainText.setText(getSDRemainSize() + "MB"); } } /** * 判断SD卡是否存在 * * @return */ private boolean isExistSDCard() { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { return true; } return false; } /** * 获取SD卡剩余空间 * * @return */ private long getSDRemainSize() { // 获取SD卡根目录文件 File file = Environment.getExternalStorageDirectory(); // 构建StatFs对象 StatFs sfs = new StatFs(file.getPath()); // 获取单个数据块的大小(Byte) long blockSize = sfs.getBlockSize(); // 获取空闲的数据块的数量 long freeBlocks = sfs.getAvailableBlocks(); // 返回SD卡空闲大小 return (freeBlocks * blockSize) / 1024 / 1024; // 单位MB } /** * 获取SD卡总容量 * * @return */ private long getSDAllSize() { // 获取SD卡根目录文件 File file = Environment.getExternalStorageDirectory(); // 构建StatFs对象 StatFs sfs = new StatFs(file.getPath()); // 获取单个数据块的大小(Byte) long blockSize = sfs.getBlockSize(); // 获取所有数据块数 long allBlocks = sfs.getBlockCount(); // 返回SD卡大小 return (allBlocks * blockSize) / 1024 / 1024; // 单位MB } }
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/main_btn_all" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取SD卡总容量" /> <TextView android:id="@+id/main_tv_all" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/main_btn_remain" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取SD卡剩余空间" /> <TextView android:id="@+id/main_tv_remain" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" /> </LinearLayout> </LinearLayout>
权限:
<!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <!-- 往SDCard写入数据权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />