`
yangguangfu
  • 浏览: 1531942 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

得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. */
	@Override
	public 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() {

			@Override
			public void onClick(View v) {
				String[] total = fileSize(getTotalExternalMemorySize());
				String[] available = fileSize(getAvailableExternalMemorySize());

				String text = "总共" + total[0] + total[1] + "\n";
				text += "可用" + available[0] + available[1];
				myTextView.setText(text);
			}
		});
		myButton2.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String[] total = fileSize(getTotalInternalMemorySize());
				String[] available = fileSize(getAvailableInternalMemorySize());

				String text = "总共" + total[0] + total[1] + "\n";
				text += "可用" + available[0] + available[1];
				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;
	}

	/* 返回为字符串数组[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;
	}
}
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics