android 获得SDCard信息
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<!-- 显示block的数量的标签控件 -->
<TextView
android:id="@+id/tv_TotalBlocks"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- 显示block的大小的标签控件 -->
<TextView
android:id="@+id/tv_BlocSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- 显示可用block的标签控件 -->
<TextView
android:id="@+id/tv_AvailaBlock"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- 显示空block的标签控件 -->
<TextView
android:id="@+id/tv_FreeBlock"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- 显示SDCard的总大小的标签控件 -->
<TextView
android:id="@+id/tv_SDTotalSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- 显示SDCard的剩余大小的标签控件 -->
<TextView
android:id="@+id/tv_SDFreeSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>
package com.example.yanlei.yl2; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView; import java.io.File; public class MainActivity extends AppCompatActivity {
private TextView mTvTotalBlocks; //SDCard上BLOCK总数
private TextView mTvBlocSize; //SDCard上每个block的SIZE
private TextView mTvAvailaBlock; //可供程序使用的Block的数量
private TextView mTvFreeBlock; //剩下的所有Block的数量(包括预留的一般程序无法使用的块)
private TextView mTvSDTotalSize; //SDCard 总容量大小MB
private TextView mTvSDFreeSize; //SDCard 剩余大小MB @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 得到布局中的控件
findView();
// 绑定控件事件
SDCardSizeTest();
} private void findView() {
// 绑定控件
mTvTotalBlocks = (TextView)findViewById(R.id.tv_TotalBlocks);
mTvBlocSize = (TextView)findViewById(R.id.tv_BlocSize);
mTvAvailaBlock = (TextView)findViewById(R.id.tv_AvailaBlock);
mTvFreeBlock = (TextView)findViewById(R.id.tv_FreeBlock);
mTvSDTotalSize = (TextView)findViewById(R.id.tv_SDTotalSize);
mTvSDFreeSize = (TextView)findViewById(R.id.tv_SDFreeSize);
} public void SDCardSizeTest() {
// 取得SDCard当前的状态
String sDcString = android.os.Environment.getExternalStorageState(); //如果当前系统有sdcard存在
if (sDcString.equals(android.os.Environment.MEDIA_MOUNTED)) {
// 取得sdcard文件路径
File pathFile = android.os.Environment
.getExternalStorageDirectory();
//得到sdcard的状态
android.os.StatFs statfs = new android.os.StatFs(pathFile.getPath()); // 获取SDCard上BLOCK总数
long nTotalBlocks = statfs.getBlockCount();
mTvTotalBlocks.setText("SDCard上BLOCK总数: "+nTotalBlocks); // 获取SDCard上每个block的SIZE
long nBlocSize = statfs.getBlockSize();
mTvBlocSize.setText("SDCard上每个bloc的SIZE:"+nBlocSize); // 获取可供程序使用的Block的数量
long nAvailaBlock = statfs.getAvailableBlocks();
mTvAvailaBlock.setText("可供程序使用的Block的数量 : " + nAvailaBlock); // 获取剩下的所有Block的数量(包括预留的一般程序无法使用的块)
long nFreeBlock = statfs.getFreeBlocks();
mTvFreeBlock.setText("剩下的所有Block的数量: " + nFreeBlock); // 计算SDCard 总容量大小MB
long nSDTotalSize = nTotalBlocks * nBlocSize / 1024 / 1024;
mTvSDTotalSize.setText("SDCard 总容量大小MB: " + nSDTotalSize +"MB"); // 计算 SDCard 剩余大小MB
long nSDFreeSize = nAvailaBlock * nBlocSize / 1024 / 1024;
mTvSDFreeSize.setText(" SDCard 剩余大小MB: " + nSDFreeSize +"MB");
}
}
}
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView; import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity { private TextView myTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); myTextView= (TextView)findViewById(R.id.myTextView);
StringBuilder log = new StringBuilder();
String inPath = getInnerSDCardPath();
String str="内置SD卡路径:" + inPath + "\r\n";
log.append("内置SD卡路径:" + inPath + "\r\n"); List<String> extPaths = getExtSDCardPath();
for (String path : extPaths) {
str=str+"外置SD卡路径:" + path + "\r\n";
log.append("外置SD卡路径:" + path + "\r\n");
}
myTextView.setText(str);
System.out.println(log.toString());
} /**
* 获取内置SD卡路径
* @return
*/
public String getInnerSDCardPath() {
return Environment.getExternalStorageDirectory().getPath();
} /**
* 获取外置SD卡路径
* @return 应该就一条记录或空
*/
public List<String> getExtSDCardPath()
{
List<String> lResult = new ArrayList<String>();
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("mount");
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
if (line.contains("extSdCard"))
{
String [] arr = line.split(" ");
String path = arr[1];
File file = new File(path);
if (file.isDirectory())
{
lResult.add(path);
}
}
}
isr.close();
} catch (Exception e) {
}
return lResult;
}
android 获得SDCard信息的更多相关文章
- android操作sdcard中的多媒体文件(二)——音乐列表的更新
android操作sdcard中的多媒体文件(二)——音乐列表的更新 原文地址 在上一篇随笔中,我介绍了如何在程序中查询sdcard内的多媒体文件,并且显示到播放列表中,但是,如果在sdcard内删除 ...
- android操作sdcard中的多媒体文件(一)——音乐列表的制作
android操作sdcard中的多媒体文件(一)——音乐列表的制作 原文地址 最近做了一个android音乐播放器,个人感觉最难的就是“后台播放”以及有关“播放列表”的部分,但是总算是找到了实现的方 ...
- I.MX6 android 获取framebuffer信息
/******************************************************************************** * I.MX6 android 获取 ...
- Android 使用SDcard进行文件的读取
平时我们需要在手机上面存储想音频,视频等等的大文件,以前学过使用File进行存储(使用File操作进行存储):由于考虑到手机本身的存储空间小,这时候我们需要把文件存储在SDcard中,今天自己也学习了 ...
- Android内存等信息
1. Linux中proc目录下文件详解 http://wenku.baidu.com/view/2ce89f00a6c30c2259019ef1.html 2. Android系统/proc目录详解 ...
- Android中日志信息的打印方式
Android中日志信息的打印方式主要有以下7种: 1)System.out(i级别) 2)System.err(w级别) 3)Log.v 4)Log.d 5)Log.i 6)Log.w 7)Log. ...
- Android 获取手机信息,设置权限,申请权限,查询联系人,获取手机定位信息
Android 获取手机信息,设置权限,申请权限,查询联系人,获取手机定位信息 本文目录: 获取手机信息 设置权限 申请权限 查询联系人 获取手机定位信息 调用高德地图,设置显示2个坐标点的位置,以及 ...
- 获取 Android APP 版本信息工具类(转载)
获取 Android APP 版本信息工具类 获取手机APP版本信息工具类 1.获取版本名称 2.获取版本号 3.获取App的名称 package com.mingyue.nanshuibeidiao ...
- android sdcard信息获取
手机存储都有两种,一种是 手机自带的存储,称为internal storage,另外一种用户额外插入的存储,称为removable storage (也就是外置sdcard的部分). removabl ...
随机推荐
- C++系统学习一:基本数据类型和变量
程序语言 程序语言最基本的特征 整型.字符型等内置类型 变量,用来为对象命名 表达式和语句,操纵上述数据类型的具体值 if等控制结构 函数,定义可供随时调用的计算单元 程序语言的扩展 自定义数据类型 ...
- Luogu P1080国王游戏(贪心)
国王游戏 题目链接:国王游戏 ps:题目数据说明了要写高精度. 这个题的答案是\(a.l * a.r < b.l * b.r\)按照这个进行排序 题解中大部分只是如何证明排序是: \(a.l * ...
- [LUOGU] P1048 采药
题目描述 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个难题.医师把他带到一个到处都是草药的山洞里对他说:" ...
- Python基础:字典(dict)与集合(set)
查找场景下与列表的性能对比 字典与集合之所以高效的原因是:内部结构都是一张哈希表. 平均情况下插入.查找和删除的时间复杂度为 O(1). 假设有数量100,000的产品列表: import time ...
- Java集合之PriorityQueue
PriorityQueue 定义 C++:priority_queue Java:PriorityQueue 创建与其基本操作 创建: PriorityQueue<Integer>=new ...
- LeetCode 637. Average of Levels in Binary Tree(层序遍历)
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...
- JavaScript内建对象-String
JavaScript中通过双引号或单引号界定一个字符串. String对象只有一个属性:length属性,得到字符串的长度. 处理字符串本身的方法 charAt(index) 返回字符串中index指 ...
- H.264编码profile & level控制
背景知识 先科普一下profile&level.(这里讨论最常用的H264) H.264有四种画质级别,分别是baseline, extended, main, high: 1.Baseli ...
- 大数据学习——hive的sql练习题
ABC三个hive表 每个表中都只有一列int类型且列名相同,求三个表中互不重复的数 create table a(age int) row format delimited fields termi ...
- Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follow up: Can
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...