android 读取SD卡文件
2013-07-11 09:31
309 查看
SD卡作为手机的扩展存储设备,在手机中充当硬盘角色,可以让我们手机存放更多的数据以及多媒体等大体积文件。因此查看SD卡的内存就跟我们查看硬盘的剩余空间一样,是我们经常操作的一件事,那么在Android开发中,我们如何能获取SD卡的内存容量呢?
首先,要获取SD卡上面的信息,必须先对SD卡有访问的权限,因此第一件事就是需要添加访问扩展设备的权限。
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission>
其次,需要判断手机上面SD卡是否插好,如果有SD卡的情况下,我们才可以访问得到并获取到它的相关信息,当然以下这个语句需要用if做判断。
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
取得sdcard文件路径
File path = Environment.getExternalStorageDirectory();
StatFs statfs = new StatFs(path.getPath());
获取block的SIZE
long blocSize = statfs.getBlockSize();
获取BLOCK数量
long totalBlocks = statfs.getBlockCount();
空闲的Block的数量
long availaBlock = statfs.getAvailableBlocks();
计算总空间大小和空闲的空间大小
储空间大小跟空闲的存储空间大小就被计算出来了。
/**
* 取得空闲sd卡空间大小
* @return
*/
public long getAvailaleSize(){
File path = Environment.getExternalStorageDirectory(); //取得sdcard文件路径
StatFs stat = new StatFs(path.getPath());
/*获取block的SIZE*/
long blockSize = stat.getBlockSize();
/*空闲的Block的数量*/
long availableBlocks = stat.getAvailableBlocks();
/* 返回bit大小值*/
return availableBlocks * blockSize;
//(availableBlocks * blockSize)/1024 KIB 单位
//(availableBlocks * blockSize)/1024 /1024 MIB单位
}
/**
* SD卡大小
* @return
*/
public long getAllSize(){
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
/*获取block的SIZE*/
long blockSize = stat.getBlockSize();
/*块数量*/
long availableBlocks = stat.getBlockCount();
/* 返回bit大小值*/
return availableBlocks * blockSize;
}
Android 文件的浏览(类似于FileDialog的功能)
首先说一下这个文件浏览的简单实现原理:
首先选择一个目录做为根目录,然后打开此目录,常用的就是使用File这个类了,如下:
File file=new File(path);
然后可以通过获取到此目录下所有文件及文件夹的列表:
如下:
File[] files = file.listFiles();
然后再根据根据得到的文件,来判断是文件夹还是文件,如果是文件夹,那么我们就将文件夹添加到列表中,如果是文件那么就把文件添加到列表中进行显示,如果需要显示图标的话,那么就需要根据文件的后缀,设置不同的图标了。我是在ListView中进行显示的。
基本代码如下:
for (File currentFile : files)
{
//判断是一个文件夹还是一个文件
if (currentFile.isDirectory())
{
currentIcon = getResources().getDrawable(R.drawable.folder);
}
else
{
//取得文件名
String fileName = currentFile.getName();
//根据文件名来判断文件类型,设置不同的图标
if (checkEndsWithInStringArray(fileName,
getResources().getStringArray(R.array.fileEndingImage)))
{
currentIcon = getResources().getDrawable(R.drawable.image);
}
else if (checkEndsWithInStringArray(fileName,
getResources().getStringArray(R.array.fileEndingWebText)))
{
currentIcon = getResources().getDrawable(R.drawable.webtext);
}
else if (checkEndsWithInStringArray(fileName,
getResources().getStringArray(R.array.fileEndingPackage)))
{
currentIcon = getResources().getDrawable(R.drawable.packed);
}
else if (checkEndsWithInStringArray(fileName,
getResources().getStringArray(R.array.fileEndingAudio)))
{
currentIcon = getResources().getDrawable(R.drawable.audio);
}
else if (checkEndsWithInStringArray(fileName,
getResources().getStringArray(R.array.fileEndingVideo)))
{
currentIcon = getResources().getDrawable(R.drawable.video);
}
else
{
currentIcon = getResources().getDrawable(R.drawable.text);
}
}
下面是根据后缀对文件的类型进行的判断:
//通过文件名判断是什么类型的文件
private boolean checkEndsWithInStringArray(String
checkItsEnd,
String[] fileEndings)
{
for(String
aEnd : fileEndings)
{
if(checkItsEnd.endsWith(aEnd))
return true;
}
return false;
}
再贴一段自己的代码,获取SD卡文件名
private void initData() {
// 判断是否有外存储设备
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File path = Environment.getExternalStorageDirectory();
File file = new File(path.getPath());
File[] files = file.listFiles();
getFileName(files);
}
}
private void getFileName(File[] files) {
for (File currentFile : files) {
// 判断是一个文件夹还是一个文件
if (currentFile.isDirectory()) {
getFileName(currentFile.listFiles());
} else {
// 取得文件名
String fileName = currentFile.getName();
// 获取.info后缀的文件
if (fileName.endsWith(".info"))
// 保存去掉后缀的文件名
names.add(fileName.substring(0, fileName.lastIndexOf(".")));
}
}
}
首先,要获取SD卡上面的信息,必须先对SD卡有访问的权限,因此第一件事就是需要添加访问扩展设备的权限。
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission>
其次,需要判断手机上面SD卡是否插好,如果有SD卡的情况下,我们才可以访问得到并获取到它的相关信息,当然以下这个语句需要用if做判断。
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
取得sdcard文件路径
File path = Environment.getExternalStorageDirectory();
StatFs statfs = new StatFs(path.getPath());
获取block的SIZE
long blocSize = statfs.getBlockSize();
获取BLOCK数量
long totalBlocks = statfs.getBlockCount();
空闲的Block的数量
long availaBlock = statfs.getAvailableBlocks();
计算总空间大小和空闲的空间大小
储空间大小跟空闲的存储空间大小就被计算出来了。
/**
* 取得空闲sd卡空间大小
* @return
*/
public long getAvailaleSize(){
File path = Environment.getExternalStorageDirectory(); //取得sdcard文件路径
StatFs stat = new StatFs(path.getPath());
/*获取block的SIZE*/
long blockSize = stat.getBlockSize();
/*空闲的Block的数量*/
long availableBlocks = stat.getAvailableBlocks();
/* 返回bit大小值*/
return availableBlocks * blockSize;
//(availableBlocks * blockSize)/1024 KIB 单位
//(availableBlocks * blockSize)/1024 /1024 MIB单位
}
/**
* SD卡大小
* @return
*/
public long getAllSize(){
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
/*获取block的SIZE*/
long blockSize = stat.getBlockSize();
/*块数量*/
long availableBlocks = stat.getBlockCount();
/* 返回bit大小值*/
return availableBlocks * blockSize;
}
Android 文件的浏览(类似于FileDialog的功能)
首先说一下这个文件浏览的简单实现原理:
首先选择一个目录做为根目录,然后打开此目录,常用的就是使用File这个类了,如下:
File file=new File(path);
然后可以通过获取到此目录下所有文件及文件夹的列表:
如下:
File[] files = file.listFiles();
然后再根据根据得到的文件,来判断是文件夹还是文件,如果是文件夹,那么我们就将文件夹添加到列表中,如果是文件那么就把文件添加到列表中进行显示,如果需要显示图标的话,那么就需要根据文件的后缀,设置不同的图标了。我是在ListView中进行显示的。
基本代码如下:
for (File currentFile : files)
{
//判断是一个文件夹还是一个文件
if (currentFile.isDirectory())
{
currentIcon = getResources().getDrawable(R.drawable.folder);
}
else
{
//取得文件名
String fileName = currentFile.getName();
//根据文件名来判断文件类型,设置不同的图标
if (checkEndsWithInStringArray(fileName,
getResources().getStringArray(R.array.fileEndingImage)))
{
currentIcon = getResources().getDrawable(R.drawable.image);
}
else if (checkEndsWithInStringArray(fileName,
getResources().getStringArray(R.array.fileEndingWebText)))
{
currentIcon = getResources().getDrawable(R.drawable.webtext);
}
else if (checkEndsWithInStringArray(fileName,
getResources().getStringArray(R.array.fileEndingPackage)))
{
currentIcon = getResources().getDrawable(R.drawable.packed);
}
else if (checkEndsWithInStringArray(fileName,
getResources().getStringArray(R.array.fileEndingAudio)))
{
currentIcon = getResources().getDrawable(R.drawable.audio);
}
else if (checkEndsWithInStringArray(fileName,
getResources().getStringArray(R.array.fileEndingVideo)))
{
currentIcon = getResources().getDrawable(R.drawable.video);
}
else
{
currentIcon = getResources().getDrawable(R.drawable.text);
}
}
下面是根据后缀对文件的类型进行的判断:
//通过文件名判断是什么类型的文件
private boolean checkEndsWithInStringArray(String
checkItsEnd,
String[] fileEndings)
{
for(String
aEnd : fileEndings)
{
if(checkItsEnd.endsWith(aEnd))
return true;
}
return false;
}
再贴一段自己的代码,获取SD卡文件名
private void initData() {
// 判断是否有外存储设备
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File path = Environment.getExternalStorageDirectory();
File file = new File(path.getPath());
File[] files = file.listFiles();
getFileName(files);
}
}
private void getFileName(File[] files) {
for (File currentFile : files) {
// 判断是一个文件夹还是一个文件
if (currentFile.isDirectory()) {
getFileName(currentFile.listFiles());
} else {
// 取得文件名
String fileName = currentFile.getName();
// 获取.info后缀的文件
if (fileName.endsWith(".info"))
// 保存去掉后缀的文件名
names.add(fileName.substring(0, fileName.lastIndexOf(".")));
}
}
}
相关文章推荐
- 转-Android 之 使用File类在SD卡中读取数据文件
- android 读取SD卡目录下文件
- android 读取SD卡文件
- Android之根据读取SD卡指定后缀名的文件
- 【Android 开发】:数据存储之File文件存储 [即SD卡的写入与读取]
- android读取内存和sd卡中的txt文件
- android 6.0 读取sd卡上文件报错,manifest已经添加权限, open failed: EACCES (Permission denied)
- Android读取SD卡中的媒体文件(MP3)
- android 读取SD卡下TXT文件
- android 中读取asset目录中文件并写入SD卡以及从SD卡中读取的简介
- Android之SD卡上的文件读取
- Android文件的保存(本地和SD卡)与读取
- Android 之 使用File类在SD卡中读取数据文件
- Android 读取SD卡指定后缀的所有文件
- android读取sd卡文件的工具类
- android 6.0 sd卡读取文件失败
- android--读取sd卡中text文件
- android读取sd卡数据库文件相关问题
- [转载]android读取sd卡文件的工具类
- 读取Android sd卡的文件