当需要访问SD卡上的文件时,需要按照如下步骤进行

*调用Environment.getExternalStorageState()判读手机上是否插入SD卡(返回MEDIA_MOUNTED则表示已经插入)
*调用Environment.getExternalStorageDirectory()获取SD卡所在的目录
*使用文件IO流相关方法读写SD卡的内容
*在AndroidManifest.xml文件中添加与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存储的更多相关文章

  1. sp,文件以及SDcard存储

    XML: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    androi ...

  2. Android--数据持久化之内部存储、Sdcard存储

    前言 之前一直在讲AndroidUI的内容,但是还没有完结,之后会慢慢补充.今天讲讲其他的,关于数据持久化的内容.对于一个应用程序而言,不可避免的要能够对数据进行存储,Android程序也不例外.而在 ...

  3. 万能存储工具类SDCard存储 /data/data/存储 assets存储 raw存储

    万能存储工具类 SDCard存储  /data/data/存储  assets存储 raw存储 粘贴过去就能够用了 <uses-permission android:name="and ...

  4. Android:StatFs类 获取系统/sdcard存储空间信息

    在存储文件时,为了保证有充足的存储空间大小,通常需要知道系统内部或者sdcard的剩余存储空间大小,这里就需要用到StatFs类. 1. 判断 SDCard 是否存在,并且是否具有可读写权限 /** ...

  5. 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 ...

  6. 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 ...

  7. 获取android SDCard存储大小

    //File path = Environment.getDataDirectory();//手机内置空间 1.获取SD卡的路径 File path = Environment.getExternal ...

  8. Android简单介绍SharedPreference,内部文件,sdcard数据存储

    SharedPreference 以xml的结构储存简单的数据,储存在data/data/程序包名/shared_prefs文件夹中 使用方式 创建对象的方式有三种 Context 的 getShar ...

  9. android 开发-文件存储之读写sdcard

    android提供对可移除的外部存储进行文件存储.在对外部sdcard进行调用的时候首先要调用Environment.getExternalStorageState()检查sdcard的可用状态.通过 ...

随机推荐

  1. iOS-(kCFStreamErrorDomainSSL, -9802)

    kCFStreamErrorDomainSSL, -9802 我是微博授权时get页面时候碰到的 其实就是http安全问题 在info.plist里添加并设置Allow Arbitrary Loads ...

  2. Yacc 与 Lex 快速入门

    Yacc 与 Lex 快速入门 Lex 与 Yacc 介绍 Lex 和 Yacc 是 UNIX 两个非常重要的.功能强大的工具.事实上,如果你熟练掌握 Lex 和 Yacc 的话,它们的强大功能使创建 ...

  3. [转]uses-permission权限列表

    android.permission.ACCESS_CHECKIN_PROPERTIES允许读写访问”properties”表在checkin数据库中,改值可以修改上传 android.permiss ...

  4. Eclipse设置:背景与字体大小和xml文件中字体大小调整

    Eclipse中代码编辑背景颜色修改:代码编辑界面默认颜色为白色.对于长期使用电脑编程的人来说,白色很刺激我们的眼睛,所以改变workspace的背景色,可以使眼睛舒服一些.设置方法如下:1.打开wi ...

  5. Maya导入Unity的教程

    原地址:http://www.cocoachina.com/gamedev/gameengine/2010/0601/1586.html 昨天已经发布了1Vr.Cn翻译的多维材质模型烘培入Unity  ...

  6. quaternion*Vector3的新理解

    原地址:http://www.cnblogs.com/88999660/p/3262656.html using UnityEngine; using System.Collections; publ ...

  7. MySQL目录

    MySQL的学习总结目录 Mysql5.7安装及配置 教你如何3分钟玩转MYSQL MySQL使用详解--根据个人学习总结 Mysql增删改 Mysql_以案例为基准之查询 MySQL之扩展(触发器, ...

  8. 12 day 1

    #include <cstdio> int i,j,m,n,t; long long f[6000][6000]; inline int min(int a,int b){ return ...

  9. scrapy和selenium结合抓取动态网页

    1.安装python (我用的是2.7版本的) 2.安装scrapy:   详情请参考 http://blog.csdn.net/wukaibo1986/article/details/8167590 ...

  10. 1.1 让CPU占用率曲线听你指挥[cpu manager]

    [本文链接] http://www.cnblogs.com/hellogiser/p/cpu-manager.html [题目] 写一个程序,让用户来决定Windows任务管理器(Task Manag ...