码迷,mamicode.com
首页 > 其他好文 > 详细

获取sdcard和内存的存储空间

时间:2016-04-13 13:12:30      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

 1 package com.example.sdcardspace;
 2 
 3 import java.io.File;
 4 
 5 import android.os.Bundle;
 6 import android.os.Environment;
 7 import android.os.StatFs;
 8 import android.app.Activity;
 9 import android.text.format.Formatter;
10 import android.view.Menu;
11 import android.widget.TextView;
12 
13 public class MainActivity extends Activity {
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_main);
19         TextView tv = (TextView)findViewById(R.id.tv);
20         
21         File path = Environment.getExternalStorageDirectory();
22         StatFs stat = new StatFs(path.getPath());
23         long blockSize = stat.getBlockSize();
24         long totalBlocks = stat.getBlockCount();
25         long availblocks = stat.getAvailableBlocks();
26         long totalSize = blockSize * totalBlocks;
27         long availSize = blockSize * availblocks;
28         String totalStr = Formatter.formatFileSize(this, totalSize);
29         String availStr = Formatter.formatFileSize(this, availSize);
30         
31         tv.setText("总空间为: " + totalStr + "\n可用空间为: " + availStr + "\n" + getMonmery());
32         
33         
34     }
35     
36     public String getMonmery()
37     {
38         File path = Environment.getDataDirectory();
39         StatFs stat = new StatFs(path.getPath());
40         long blockSize = stat.getBlockSize();
41         long totalBlocks = stat.getBlockCount();
42         long availblocks = stat.getAvailableBlocks();
43         long totalSize = blockSize * totalBlocks;
44         long availSize = blockSize * availblocks;
45         String totalStr = Formatter.formatFileSize(this, totalSize);
46         String availStr = Formatter.formatFileSize(this, availSize);
47         return "内存块的总容量为: " + totalStr + "\n可用空间为: " + availStr;
48     }
49 
50 }

 

获取sdcard和内存的存储空间

标签:

原文地址:http://www.cnblogs.com/zhongyinghe/p/5386503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!