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的可用状态.通过 ...
随机推荐
- Objective-C 之优雅的命名(转)
There are only two hard things in Computer Science: cache invalidation and naming things. 在计算机科学中只有两 ...
- javascript 对象(DOM)document window history
Javascript对象 目录: window对象 document对象 history对象 navigator对象 window对象 所有浏览器都支持window对象,它表示浏览器窗口. 所有jav ...
- Kik CEO Ted Livingston发博称要成为西方的微信?
加拿大手机聊天应用Kik是一款手机通信录的社交软件,和Snapchat.微信相似,上个月刚拿到3830万美元融资.近日,Kik CEO Ted Livingston在medium博客上发表了the r ...
- IE下的haslayout特性
什么是hasLayout?hasLayout是IE特有的一个属性.很多的ie下的css bug都与其息息相关.在ie中,一个元素要么自己对自身的内容进行计算大小和组织,要么依赖于父元素来计算尺寸和组织 ...
- OpenStack 的Nova组件详解
Open Stack Compute Infrastructure (Nova) Nova是OpenStack云中的计算组织控制器.支持OpenStack云中实例(instances)生命周期的所有活 ...
- HDOJ 2544
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- 【Spring】Spring系列6之Spring整合Hibernate
6.Spring整合Hibernate 6.1.准备工作 6.2.示例 com.xcloud.entities.book com.xcloud.dao.book com.xcloud.service. ...
- Python发布Django项目的pyc版脚本
import os import sys from py_compile import compile #print "argvs:",sys.argv if len(sys.ar ...
- placement new讲解
[本文链接] http://www.cnblogs.com/hellogiser/p/placement-new.html [分析] 首先我们区分下几个容易混淆的关键词:new.operator ne ...
- 利用FFmpeg生成视频缩略图 2.1.8
1.下载FFmpeg文件包,解压包里的\bin\下的文件解压到 D:\ffmpeg\ 目录下. 下载地址 http://ffmpeg.zeranoe.com/builds/win32/static/ ...