得SDCard,手机,存储空间,可用空间(付源码)
package com.yang.sdcard;import java.io.File;import java.text.DecimalFormat;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 SdcardActivity extends Activity {private Button myButton, myButton2;private TextView myTextView;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);myButton = (Button) findViewById(R.id.myButton);myButton2 = (Button) findViewById(R.id.myButton2);myTextView = (TextView) findViewById(R.id.myTextView);myButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String[] total = fileSize(getTotalExternalMemorySize());String[] available = fileSize(getAvailableExternalMemorySize());String text = "总共" + total + total + "\n";text += "可用" + available + available;myTextView.setText(text);}});myButton2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String[] total = fileSize(getTotalInternalMemorySize());String[] available = fileSize(getAvailableInternalMemorySize());String text = "总共" + total + total + "\n";text += "可用" + available + available;myTextView.setText(text);}});}// 这个是手机内存的总空间大小public static long getTotalInternalMemorySize() {File path = Environment.getDataDirectory();StatFs stat = new StatFs(path.getPath());long blockSize = stat.getBlockSize();long totalBlocks = stat.getBlockCount();return totalBlocks * blockSize;}// 这个是手机内存的可用空间大小public static long getAvailableInternalMemorySize() {File path = Environment.getDataDirectory();StatFs stat = new StatFs(path.getPath());long blockSize = stat.getBlockSize();long availableBlocks = stat.getAvailableBlocks();return availableBlocks * blockSize;}// 这个是外部存储的总空间大小public static long getAvailableExternalMemorySize() {long availableExternalMemorySize = 0;if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {File path = Environment.getExternalStorageDirectory();StatFs stat = new StatFs(path.getPath());long blockSize = stat.getBlockSize();long availableBlocks = stat.getAvailableBlocks();availableExternalMemorySize = availableBlocks * blockSize;}else if (Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED)) {availableExternalMemorySize = -1;}return availableExternalMemorySize;}// 这个是外部存储的总空间大小public static long getTotalExternalMemorySize() {long totalExternalMemorySize = 0;if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {File path = Environment.getExternalStorageDirectory();StatFs stat = new StatFs(path.getPath());long blockSize = stat.getBlockSize();long totalBlocks = stat.getBlockCount();totalExternalMemorySize = totalBlocks * blockSize;} else if (Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED)) {totalExternalMemorySize = -1;}return totalExternalMemorySize;}/* 返回为字符串数组为大小为单位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;result = formatter.format(size);result = str;return result;}}
页:
[1]