SD卡作为手机的常用外设,在实际开发中必须考虑到它的相关状态对APP的影响,尤其是涉及到读写时,很容易测出bug,当SD卡快满的时候,写入前必须要有一个判断
Enviroment类 包含SD卡状态、各种文件的下载路径等信息,下面有各种获取路径的方法
getExternalStorageDirectory()获取当前外部存储的目录,由于现在的SD卡目录可能不在系统默认的三个路径中,这个方法可以直接找到。
StatFs 去看文件系统状态的类
getBlockSizeLong() 最小簇(存储单元)大小 getBlockCountLong()总大小 getAvailableBlocksLong()可用大小,这是api18以上使用的,要注意
另外在计算大小的时候,不能直接通过字节数除以1024这样来算,如果不到1024,那就会出现0,要用到系统的格式化方法formatter,可以看看他的源码
private static String formatFileSize(Context context, long number, boolean shorter) {
if (context == null) {
return "";
}
float result = number;
int suffix = com.android.internal.R.string.byteShort;
if (result > 900) {
suffix = com.android.internal.R.string.kilobyteShort;
result = result / 1024;
}
if (result > 900) {
suffix = com.android.internal.R.string.megabyteShort;
result = result / 1024;
}
if (result > 900) {
suffix = com.android.internal.R.string.gigabyteShort;
result = result / 1024;
}
if (result > 900) {
suffix = com.android.internal.R.string.terabyteShort;
result = result / 1024;
}
if (result > 900) {
suffix = com.android.internal.R.string.petabyteShort;
result = result / 1024;
}
String value;
if (result < 1) {
value = String.format("%.2f", result);
} else if (result < 10) {
if (shorter) {
value = String.format("%.1f", result);
} else {
value = String.format("%.2f", result);
}
} else if (result < 100) {
if (shorter) {
value = String.format("%.0f", result);
} else {
value = String.format("%.2f", result);
}
} else {
value = String.format("%.0f", result);
}
return context.getResources().
getString(com.android.internal.R.string.fileSizeSuffix,
value, context.getString(suffix));
}
几个方法的使用
layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sdcard.MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/tv_available"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可用空间" />
<TextView
android:id="@+id/tv_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="总空间" />
</LinearLayout>
实现
public class MainActivity extends Activity {
long blockSize;
long totalBlocks;
long availableBlocks;
TextView tv_available = (TextView) findViewById(R.id.tv_available);
TextView tv_total = (TextView) findViewById(R.id.tv_total);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取SD卡路径
File path = Environment.getExternalStorageDirectory();
//这张卡的状态信息
StatFs stat = new StatFs(path.getPath());
//获取大小,但是这样的大小是无法显示到屏幕上的,需要第四步
//在高于18的api,要用带long的
if (Build.VERSION.SDK_INT >Build.VERSION_CODES.JELLY_BEAN_MR2) {
blockSize = stat.getBlockSizeLong();
totalBlocks = stat.getBlockCountLong();
availableBlocks = stat.getAvailableBlocksLong();
}else {
blockSize = stat.getBlockSize();
totalBlocks = stat.getBlockCount();
availableBlocks = stat.getAvailableBlocks();
}
/*log跟踪下
Log.i("sdcard", availableBlocks * blockSize+"");
Log.i("sdcard", totalBlocks * blockSize+"");*/
//通过返回值计算,把字节转换成M
//如果直接除以1024来计算,会有bug,如果不到1024就会出现0
String sdavail = formatSize(availableBlocks * blockSize);
String sdtotal = formatSize(totalBlocks * blockSize);
tv_available.setText(sdavail);
tv_total.setText(sdtotal);
}
private String formatSize(long size) {
return Formatter.formatFileSize(this, size);
}
}