SDCard存储
当需要访问SD卡上的文件时,需要按照如下步骤进行
// 判断SDCard是否挂载
public static boolean isSDCardMounted() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return true;
}
return false;
}
// 获得SDCard的根目录 如/storage/sdcard/
public static String getSDCardBaseDir() {
if (isSDCardMounted()) {
File dir = Environment.getExternalStorageDirectory();
return dir.getAbsolutePath();
}
return null;
}
// 获得SD卡的全部空间大小(单位:M)
public static long getSDCardSize() {
if (isSDCardMounted()) {
String dir = getSDCardBaseDir();
StatFs statFs = new StatFs(dir); // StatFs是C语言引入过来的
long blockCount = statFs.getBlockCountLong(); // 有多少块
long blockSize = statFs.getBlockSizeLong(); // 每块有多大
return blockCount * blockSize / 1024 / 1024;
}
return 0;
}
// 获取SDCard空闲空间的大小(单位:M) (有多少空间还没被占用的)
public static long getSDCardFreeSize() {
if (isSDCardMounted()) {
String dir = getSDCardBaseDir();
StatFs statFs = new StatFs(dir); // StatFs是C语言引入过来的
long freeBlockCount = statFs.getFreeBlocksLong(); // 空闲的块
long blockSize = statFs.getBlockSizeLong(); // 每块有多大
return freeBlockCount * blockSize / 1024 / 1024;
}
return 0;
}
// 获取SDCard可用空间的大小(单位:M) (有多少空间你可以使用的)
public static long getSDCardAvailSize() {
if (isSDCardMounted()) {
String dir = getSDCardBaseDir();
StatFs statFs = new StatFs(dir); // StatFs是C语言引入过来的
long availBlockCount = statFs.getAvailableBlocksLong(); // 可用的块
long blockSize = statFs.getBlockSizeLong(); // 每块有多大
return availBlockCount * blockSize / 1024 / 1024;
}
return 0;
}
// 往SDCard公有目录下保存文件 (九大公有目录中的一个,具体由type指定) /storage/sdcard/{type}
public static boolean saveData2SDCardPublicDir(byte[] data, String type, String filename) {
if (isSDCardMounted()) {
// 文件名:/storage/sdcard/Musics/111.txt
// 文件名:getSDCardBaseDir()/{type}/{filename}
String baseDir = getSDCardBaseDir();
String file = baseDir + File.separator + type + "/" + filename;
try {
OutputStream os = new FileOutputStream(file);
os.write(data);
os.close();
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
// 往SDCard的自定义目录中保存数据 /storage/sdcard/{dir}
public static boolean saveData2SDCardCustomDir(byte[] data, String dir, String filename) {
if (isSDCardMounted()) {
// /storage/sdcard/{dir}
// getSDCardBaseDir()/{dir}/{filename}
String baseDir = getSDCardBaseDir(); // SDCard目录
String path = baseDir + "/" + dir; // SDCard中自定义的目录
// 如果path不存在的话 需要创建目录
File customPath = new File(path);
if (!customPath.exists()) {
customPath.mkdir();
}
String file = path + "/" + filename; // SDCard中自定义目录中的文件
try {
OutputStream os = new FileOutputStream(file);
os.write(data);
os.close();
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
// :往SDCard的私有File目录下保存文件
// /storage/sdcard/Android/data/包名/files/{type}/{filename}
public static boolean saveData2SDCardPrivateFileDir(byte[] data, String type, String filename, Context context) {
if (isSDCardMounted()) {
// storage/sdcard/Android/data/包名/files/{type}/{filename}
File path = context.getExternalFilesDir(type);
if (!path.exists()) {
path.mkdir();
}
String file = path.getAbsolutePath() + "/" + filename; // 文件路径
try {
OutputStream os = new FileOutputStream(file);
os.write(data);
os.close();
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
// 往SDCard的私有Cache目录下保存文件 /storage/sdcard/Android/data/包名/cache/{filename}
public static boolean saveData2SDCardPrivateCacheDir(byte[] data, String filename, Context context) {
if (isSDCardMounted()) {
// storage/sdcard/Android/data/包名/cache/{filename}
File path = context.getExternalCacheDir();
if (!path.exists()) {
path.mkdir();
}
String file = path.getAbsolutePath() + "/" + filename;
try {
OutputStream os = new FileOutputStream(file);
os.write(data);
os.close();
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
// 往SDCard的私有Cache目录下保存图像 /storage/sdcard/Android/data/包名/cache/{filename} xxx.png xxx.jpg
public static boolean saveBitmap2SDCardPrivateCacheDir(Bitmap bitmap,
String filename, Context context) {
if(isSDCardMounted()) {
// 路径/storage/sdcard/Android/data/包名/cache/{filename}
File path = context.getExternalCacheDir();
if(!path.exists()) {
path.mkdir();
}
String file = path.getAbsolutePath() + "/" + filename; // bmp、jpg
try {
OutputStream os = new FileOutputStream(file);
// 图片转换成byte[] 下面的代码代替os.write(...);
// 判断到底是哪种图片的格式(jpg、png)
if(filename.endsWith(".jpg") || filename.endsWith(".JPG")) {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
} else if (filename.endsWith(".png") || filename.endsWith(".PNG")) {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
}
os.close();
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
// 从SDCard读取指定文件 /storage/sdcard/{filePath}
public static byte[] loadFileFromSDCard(String filePath) {
if(isSDCardMounted()) {
// /storage/sdcard/{filePath}
// getSDCardBaseDir()/{filePath}
String path = getSDCardBaseDir();
String file = path + "/" + filePath;
try {
InputStream is = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int ret;
while(true) {
ret = is.read(buffer);
if(ret < 0) {
break;
}
baos.write(buffer, 0, ret);
}
return baos.toByteArray(); // 返回字节数组
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
// 从SDCard读取Bitmap并返回 /storage/sdcard/{filePath}
public static Bitmap loadBitmapFromSDCard(String filePath) {
if(isSDCardMounted()) {
// 由于传入的参数已经是全路径了 因此不需要加上getSDCardBaseDir()
try {
InputStream is = new FileInputStream(filePath);
byte[] buffer = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int ret;
while(true) {
ret = is.read(buffer);
if(ret < 0) {
break;
}
baos.write(buffer, 0, ret);
}
byte[] data = baos.toByteArray();
return BitmapFactory.decodeByteArray(data, 0, data.length);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
SDCard存储的更多相关文章
- sp,文件以及SDcard存储
XML: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" androi ...
- Android--数据持久化之内部存储、Sdcard存储
前言 之前一直在讲AndroidUI的内容,但是还没有完结,之后会慢慢补充.今天讲讲其他的,关于数据持久化的内容.对于一个应用程序而言,不可避免的要能够对数据进行存储,Android程序也不例外.而在 ...
- 万能存储工具类SDCard存储 /data/data/存储 assets存储 raw存储
万能存储工具类 SDCard存储 /data/data/存储 assets存储 raw存储 粘贴过去就能够用了 <uses-permission android:name="and ...
- Android:StatFs类 获取系统/sdcard存储空间信息
在存储文件时,为了保证有充足的存储空间大小,通常需要知道系统内部或者sdcard的剩余存储空间大小,这里就需要用到StatFs类. 1. 判断 SDCard 是否存在,并且是否具有可读写权限 /** ...
- Android中StatFs获取系统/sdcard存储(剩余空间)大小
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- Android学习笔记之数据的Sdcard存储方法及操作sdcard的工具类
FileService.java也就是操作sdcard的工具类: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...
- 获取android SDCard存储大小
//File path = Environment.getDataDirectory();//手机内置空间 1.获取SD卡的路径 File path = Environment.getExternal ...
- Android简单介绍SharedPreference,内部文件,sdcard数据存储
SharedPreference 以xml的结构储存简单的数据,储存在data/data/程序包名/shared_prefs文件夹中 使用方式 创建对象的方式有三种 Context 的 getShar ...
- android 开发-文件存储之读写sdcard
android提供对可移除的外部存储进行文件存储.在对外部sdcard进行调用的时候首先要调用Environment.getExternalStorageState()检查sdcard的可用状态.通过 ...
随机推荐
- JAVA如何调用C/C++方法
JAVA如何调用C/C++方法 2013-05-27 JAVA以其跨平台的特性深受人们喜爱,而又正由于它的跨平台的目的,使得它和本地机器的各种内部联系变得很少,约束了它的功能.解决JAVA对本地操作的 ...
- linux查看内核版本、系统版本、系统位数(32or64)
linux查看内核版本.系统版本.系统位数(32or64) 2011-05-01 22:05:12 标签:linux 内核版本 休闲 系统版本 系统位数 1. 查看内核版本命令: 1) [root@ ...
- Merge Sort
#include<stdlib.h> #include<stdio.h> void Merge( int source[] , int temp[] , int start , ...
- FineUI第七天---文件上传
文件上传的方式: 控件的一些常用属性: ButtonText:按钮文本. ButtonOnly:是否只显示按钮,不显示只读输入框. ButtonIcon:按钮图标. ButtonIconUrl: ...
- UCloud可用区的设计理念及功能图文详解
导读 过去的几个月内,UCloud对自身的云计算基础架构进行了全面升级,于日前宣布基础架构全面支持地域和可用区,并将可用区项目命名为Sixshot.通过这两层的设计架构来组织云服务,可以为用户提供高可 ...
- unity3d camera.culling mask
原地址:http://www.cnblogs.com/88999660/archive/2013/03/14/2959439.html 官方文档对CullingMask的注释只是说了通过位移运算符,可 ...
- UIView中UIButton设置监听
红色框框是一个uibutton _priceValueLabel是他的父视图, 必须要把button的父视图设置userInteractionEnabled = YES, button的 监听才会生效 ...
- The server does not support version 3.0 of the J2EE Web module specification
1.错误: 在eclipse中使用run->run on server的时候,选择tomcat6会报错误:The server does not support version 3.0 of t ...
- Python中请使用isinstance()判断变量类型
一.isinstance() 在Python中可以使用type()与isinstance()这两个函数判断对象类型,而isinstance()函数的使用上比type更加方便. # coding=utf ...
- discuz 帖子模块用到的表及自动发帖函数
最近在做一个discuz的插件,由于需要程序自动生成并调用discuz已经存在插件的帖子.然而这就相当于自动发帖的功能了.网上找了一下,大部分都是通过curl模拟登陆,模拟发帖的,这显然不满足我的要求 ...