<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <!-- 显示block的数量的标签控件 --> <TextView android:id="@+id/tv_TotalBlocks" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- 显示block的大小的标签控件 --> <TextView android:id="@+id/tv_BlocSize" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- 显示可用block的标签控件 --> <TextView android:id="@+id/tv_AvailaBlock" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- 显示空block的标签控件 --> <TextView android:id="@+id/tv_FreeBlock" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- 显示SDCard的总大小的标签控件 --> <TextView android:id="@+id/tv_SDTotalSize" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- 显示SDCard的剩余大小的标签控件 --> <TextView android:id="@+id/tv_SDFreeSize" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
package com.example.yanlei.yl2; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; import java.io.File; public class MainActivity extends AppCompatActivity { private TextView mTvTotalBlocks; //SDCard上BLOCK总数 private TextView mTvBlocSize; //SDCard上每个block的SIZE private TextView mTvAvailaBlock; //可供程序使用的Block的数量 private TextView mTvFreeBlock; //剩下的所有Block的数量(包括预留的一般程序无法使用的块) private TextView mTvSDTotalSize; //SDCard 总容量大小MB private TextView mTvSDFreeSize; //SDCard 剩余大小MB @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 得到布局中的控件 findView(); // 绑定控件事件 SDCardSizeTest(); } private void findView() { // 绑定控件 mTvTotalBlocks = (TextView)findViewById(R.id.tv_TotalBlocks); mTvBlocSize = (TextView)findViewById(R.id.tv_BlocSize); mTvAvailaBlock = (TextView)findViewById(R.id.tv_AvailaBlock); mTvFreeBlock = (TextView)findViewById(R.id.tv_FreeBlock); mTvSDTotalSize = (TextView)findViewById(R.id.tv_SDTotalSize); mTvSDFreeSize = (TextView)findViewById(R.id.tv_SDFreeSize); } public void SDCardSizeTest() { // 取得SDCard当前的状态 String sDcString = android.os.Environment.getExternalStorageState(); //如果当前系统有sdcard存在 if (sDcString.equals(android.os.Environment.MEDIA_MOUNTED)) { // 取得sdcard文件路径 File pathFile = android.os.Environment .getExternalStorageDirectory(); //得到sdcard的状态 android.os.StatFs statfs = new android.os.StatFs(pathFile.getPath()); // 获取SDCard上BLOCK总数 long nTotalBlocks = statfs.getBlockCount(); mTvTotalBlocks.setText("SDCard上BLOCK总数: "+nTotalBlocks); // 获取SDCard上每个block的SIZE long nBlocSize = statfs.getBlockSize(); mTvBlocSize.setText("SDCard上每个bloc的SIZE:"+nBlocSize); // 获取可供程序使用的Block的数量 long nAvailaBlock = statfs.getAvailableBlocks(); mTvAvailaBlock.setText("可供程序使用的Block的数量 : " + nAvailaBlock); // 获取剩下的所有Block的数量(包括预留的一般程序无法使用的块) long nFreeBlock = statfs.getFreeBlocks(); mTvFreeBlock.setText("剩下的所有Block的数量: " + nFreeBlock); // 计算SDCard 总容量大小MB long nSDTotalSize = nTotalBlocks * nBlocSize / 1024 / 1024; mTvSDTotalSize.setText("SDCard 总容量大小MB: " + nSDTotalSize +"MB"); // 计算 SDCard 剩余大小MB long nSDFreeSize = nAvailaBlock * nBlocSize / 1024 / 1024; mTvSDFreeSize.setText(" SDCard 剩余大小MB: " + nSDFreeSize +"MB"); } } }
import android.os.Bundle; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import java.io.BufferedReader; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private TextView myTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myTextView= (TextView)findViewById(R.id.myTextView); StringBuilder log = new StringBuilder(); String inPath = getInnerSDCardPath(); String str="内置SD卡路径:" + inPath + " "; log.append("内置SD卡路径:" + inPath + " "); List<String> extPaths = getExtSDCardPath(); for (String path : extPaths) { str=str+"外置SD卡路径:" + path + " "; log.append("外置SD卡路径:" + path + " "); } myTextView.setText(str); System.out.println(log.toString()); } /** * 获取内置SD卡路径 * @return */ public String getInnerSDCardPath() { return Environment.getExternalStorageDirectory().getPath(); } /** * 获取外置SD卡路径 * @return 应该就一条记录或空 */ public List<String> getExtSDCardPath() { List<String> lResult = new ArrayList<String>(); try { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("mount"); InputStream is = proc.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { if (line.contains("extSdCard")) { String [] arr = line.split(" "); String path = arr[1]; File file = new File(path); if (file.isDirectory()) { lResult.add(path); } } } isr.close(); } catch (Exception e) { } return lResult; }