六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 26|回复: 0

得SDCard,手机,存储空间,可用空间(付源码)

[复制链接]

升级  37.33%

30

主题

30

主题

30

主题

秀才

Rank: 2

积分
106
 楼主| 发表于 2013-1-28 18:45:27 | 显示全部楼层 |阅读模式
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[0] + total[1] + "\n";text += "可用" + available[0] + available[1];myTextView.setText(text);}});myButton2.setOnClickListener(new View.OnClickListener() {@Overridepublic 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;}}  
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表