您的位置:首页 > 移动开发 > Android开发

Android获取SD卡内存空间大小实例

2013-08-24 10:18 363 查看
[考察知识点]:

A:进度条max属性

B:进度条设置方法

C:判断是否存在内存卡

D:获取可用和全部内存大小

E:DecimalFormat初始化字符串 例如:

   10000 就是 1,000

1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/strButton"
>
</Button>
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</TextView>
<ProgressBar
android:id="@+id/myProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="0">
</ProgressBar>
</LinearLayout>


2

package dfzy.EX045;

import java.io.File;
import java.text.DecimalFormat;

import dfzy.EX045.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class EX045 extends Activity
{
private Button myButton;
private ProgressBar myProgressBar;
private TextView myTextView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

myButton = (Button) findViewById(R.id.myButton);
myProgressBar = (ProgressBar) findViewById(R.id.myProgressBar);
myTextView = (TextView) findViewById(R.id.myTextView);

myButton.setOnClickListener(new Button.OnClickListener()
{

@Override
public void onClick(View arg0)
{
showSize();
}

});

}

private void showSize()
{
/*  将TextView及ProgressBar设置为空值及0 */
myTextView.setText("");
myProgressBar.setProgress(0);
/* 判断存储卡是否插入 */
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
/* 取得SD CARD文件路径一般是/sdcard */
File path = Environment.getExternalStorageDirectory();

/* StatFs看文件系统空间使用状况 */
StatFs statFs = new StatFs(path.getPath());
/* Block的size*/
long blockSize = statFs.getBlockSize();
/* 总Block数量 */
long totalBlocks = statFs.getBlockCount();
/* 已使用的Block数量 */
long availableBlocks = statFs.getAvailableBlocks();

String[] total = fileSize(totalBlocks * blockSize);
String[] available = fileSize(availableBlocks * blockSize);

/* getMax取得在main.xml里ProgressBar设置的最大值 */
int ss = Integer.parseInt(available[0]) * myProgressBar.getMax()
/ Integer.parseInt(total[0]);

myProgressBar.setProgress(ss);
String text = "总共" + total[0] + total[1] + "\n";
text += "可用" + available[0] + available[1];
myTextView.setText(text);

} else if (Environment.getExternalStorageState().equals(
Environment.MEDIA_REMOVED))
{
String text = "SD CARD已删除";
myTextView.setText(text);
}
}

/*返回为字符串数组[0]为大小[1]为单位KB或MB*/
private String[] fileSize(long size)
{
String str = "";
if (size >= 1024)
{
str = "KB";
size /= 1024;
if (size >= 1024)
{
str = "MB";
size /= 1024;
}
}

DecimalFormat formatter = new DecimalFormat();
/* 每3个数字用,分隔如:1,000 */
formatter.setGroupingSize(3);
String result[] = new String[2];
result[0] = formatter.format(size);
result[1] = str;

return result;
}

}


3

效果图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: