package com.zyh.sdcardHelper;

 import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter; public class SDCardHelper {
//判断SD卡是否被挂载
public static boolean isSDCardMounted(){
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
} //获取SDCard的绝对路径目录
public static String getSDCardBaseDir(){
if(isSDCardMounted()){
return Environment.getExternalStorageDirectory().getAbsolutePath();
}
return null;
} //获取SDCard总大小
public static String getSDCardTotalSize(Context context){
if(isSDCardMounted()){
StatFs fs = new StatFs(getSDCardBaseDir());
long blockSize = fs.getBlockSize();
long totalBlocks = fs.getBlockCount();
long totalSize = blockSize * totalBlocks;
return Formatter.formatFileSize(context, totalSize);
}
return null;
} //获取sd卡的剩余空间
public static String getSDCardFreeSize(Context context){
if(isSDCardMounted()){
StatFs fs = new StatFs(getSDCardBaseDir());
long blockSize = fs.getBlockSize();
long freeBlocks = fs.getFreeBlocks();
long freeSize = blockSize * freeBlocks;
return Formatter.formatFileSize(context, freeSize);
}
return null;
} //获取sd卡的可用空间
public static String getSDCardAvailableSize(Context context){
if(isSDCardMounted()){
StatFs fs = new StatFs(getSDCardBaseDir());
long blockSize = fs.getBlockSize();
long availableBlocks = fs.getFreeBlocks();
long availableSize = blockSize * availableBlocks;
return Formatter.formatFileSize(context, availableSize);
}
return null;
} //往sd卡的公有目录下保存数据进文件
public static boolean saveFileToSDCardPublicDir(byte[] data, String type, String fileName){
BufferedOutputStream bos = null;
if(isSDCardMounted()){
try {
File file = Environment.getExternalStoragePublicDirectory(type);
bos = new BufferedOutputStream(new FileOutputStream(new File(file,fileName)));
bos.write(data);
bos.flush();
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return false;
} //往sd卡用户自定义目录中保存数据进文件
public static boolean savaFileToSDCardCustomDir(byte[] data, String dir, String fileName){
BufferedOutputStream bos = null;
if(isSDCardMounted()){
try {
File file = new File(getSDCardBaseDir() + File.separator + dir);
if(!file.exists()){
file.mkdirs();//递归创建自定义目录
}
bos = new BufferedOutputStream(new FileOutputStream(new File(file,fileName)));
bos.write(data);
bos.flush();
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return false;
} //往sd卡私有Files目录下保存数据进文件
//注意如果不加<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>这个权限,则报空指针异常
//type可用为null
public static boolean saveFileToSDCardPrivateFilesDir(byte[] data, String type, String fileName, Context context){
BufferedOutputStream bos = null;
if(isSDCardMounted()){
try {
File file = context.getExternalFilesDir(type);
bos = new BufferedOutputStream(new FileOutputStream(new File(file,fileName)));
bos.write(data);
bos.flush();
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return false;
} //往sd卡的私有Cache目录下保存数据进文件
//cache目录和files目录是同一级的
public static boolean saveFileToSDCardPrivateCacheDir(byte[] data, String fileName, Context context){
BufferedOutputStream bos = null;
if(isSDCardMounted()){
try {
File file = context.getExternalCacheDir();
bos = new BufferedOutputStream(new FileOutputStream(new File(file,fileName)));
bos.write(data);
bos.flush();
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return false;
} //把bitmap保存在sd卡的私有cache目录中
public static boolean saveBitmapToSDCardPrivateCacheDir(Bitmap bitmap,String fileName, Context context){
if(isSDCardMounted()){
BufferedOutputStream bos = null;
File file = context.getExternalCacheDir();
try {
bos = new BufferedOutputStream(new FileOutputStream(new File(file,fileName)));
//100表示压缩率为0
if(fileName != null && (fileName.endsWith(".png") || fileName.endsWith(".PNG"))){
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
}else{
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
}
bos.flush();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bos != null){
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return true;
}
return false;
} //从sd卡的路径中获取文件的内容
public static byte[] readFileFromSDCard(String filePath){
BufferedInputStream bis = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
bis = new BufferedInputStream(new FileInputStream(new File(filePath)));
byte[] buffer = new byte[8 * 1024];
int hasRead = 0;
while((hasRead = bis.read(buffer)) != -1){
baos.write(buffer, 0, hasRead);
baos.flush();
}
return baos.toByteArray();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
baos.close();
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
} //从sd卡中获取Bitmap文件
public static Bitmap loadBitmapFromSDCard(String filePath){
byte[] data = readFileFromSDCard(filePath);
if(data != null){
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
if(bitmap != null){
return bitmap;
}
}
return null;
} //获取sd卡的公有目录路径
public static String getSDCardPublicDir(String type){
return Environment.getExternalStoragePublicDirectory(type).toString(); } //获取sd卡私有cache目录路径
public static String getSDCardPrivateCacheDir(Context context){
return context.getExternalCacheDir().getAbsolutePath();
} //sd卡私有files目录下的路径
public static String getSDCardPrivateFilesDir(Context context, String type){
return context.getExternalFilesDir(type).getAbsolutePath();
} //从sd卡中删除文件
public static boolean removeFileFromSDCard(String filePath){
File file = new File(filePath);
if(file.exists()){
try {
file.delete();
return true;
} catch (Exception e) {
return false;
}
}
return false;
} }

参考:http://www.androidchina.net/4106.html

SDCard助手类的更多相关文章

  1. ADO.NET数据库操作助手类

    SQL语句操作增删查改助手类 using System; using System.Collections.Generic; using System.Configuration; using Sys ...

  2. 【C#】SQL数据库助手类2.0(自用)

    using System; using System.Collections.Generic; using System.Configuration; using System.Data; using ...

  3. AES加密解密 助手类 CBC加密模式

    "; string result1 = AESHelper.AesEncrypt(str); string result2 = AESHelper.AesDecrypt(result1); ...

  4. Yii2 数组助手类arrayHelper

    数组助手类 ArrayHelper 1.什么是数组助手类 Yii 数组助手类提供了额外的静态方法,让你更高效的处理数组. a.获取值(getValue) class User { public $na ...

  5. WorldWind源码剖析系列:代理助手类ProxyHelper

    代理助手类ProxyHelper通过平台调用的互操作技术封送了若干Win32结构体和函数.该类类图如下. 提供的主要处理方法基本上都是静态函数,简要描述如下: 内嵌类型WINHTTP_AUTOPROX ...

  6. WorldWind源码剖析系列:图像助手类ImageHelper

    图像助手类ImageHelper封装了对各种图像的操作.该类类图如下. 提供的主要处理方法基本上都是静态函数,简要描述如下: public static bool IsGdiSupportedImag ...

  7. php 数组助手类

    ArrayHelper.php <?php /** * php 数组助手类 * Class ArrayHelper * @package app\helper */ class ArrayHel ...

  8. 数据库助手类 DBHelper

    using System; using System.Collections.Generic; using System.Text; using System.Configuration; using ...

  9. Yii的数组助手类

    获取值 用原生PHP从一个对象.数组.或者包含这两者的一个复杂数据结构中获取数据是非常繁琐的. 你首先得使用isset 检查 key 是否存在, 然后如果存在你就获取它,如果不存在, 则提供一个默认返 ...

随机推荐

  1. 页面布局之BFC 微微有点坑

    一.什么是BFC: 在解释 BFC 是什么之前,需要先介绍 Box.Formatting Context的概念. Box: CSS布局的基本单位 Box 是 CSS 布局的对象和基本单位, 直观点来说 ...

  2. 3.5 用NPOI操作EXCEL--巧妙使用Excel Chart

    在NPOI中,本身并不支持Chart等高级对象的创建,但通过l模板的方式可以巧妙地利用Excel强大的透视和图表功能,请看以下例子. 首先建立模板文件,定义两列以及指向此区域的名称“sales”: 创 ...

  3. sql 和 nosql 说明

    在传统的数据库中, 数据库的格式是由表(table).行(row).字段(field)组成的.表有固定的结构,规定了每行有哪些字段,在创建时被定义,之后修改很困难.行的格式是相同的,由若干个固定的字段 ...

  4. JavaSE学习总结第19天_IO流1

      19.01  集合的特点和数据结构总结 HashSet.HashMap.Hashtable判断元素唯一性的方式: 通过对象的hashCode和equals方法来完成元素唯一性 如果对象的hashC ...

  5. 搭建zend framework1开发环境

    1.和常规开发大致相同,首先下载zend framework1,下载地址如下 http://www.zendframework.com/downloads/latest 挑选其中一个下载,我下载的是f ...

  6. Hbase split的过程以及解发条件

    一.Split触发条件   1.  有任一一个Hfile的大小超过默认值10G时,都会进行split    2.  达到这个值不在拆分,默认为int_max,不进行拆分       3.compact ...

  7. ajax是怎么发请求的和浏览器发的请求一样吗?cookie

    下午设置cookie时出现了个问题 用ajax发的post请求php,在php的方法里设置了cookie,然后在浏览器请求的php里打印cookie值但是一直获取不到cookie的值 分析: 1.aj ...

  8. NSNumber与NSInteger的区别

    Objective-C 支持的类型有两种:基本类型 和  类. 基本类型,如同C 语言中的 int 类型一样,拿来就可以直接用. 而类在使用时,必须先创建一个对象,再为对象分配空间,接着做初始化和赋值 ...

  9. (IOS)数据持久化

    1.属性列表序列化 2.模型对象归档 3.嵌入式SQLite3 4.Core Data 5.应用程序设置 6.UIDocument管理文档存储 7.iCloud Demo界面: 1.属性列表序列化 即 ...

  10. Archive for required library: ‘WebContent/WEB-INF/lib/xxx.jar cannot&n

    今天导入一个项目到eclipse,出现感叹号,而且报1. Archive for required library: ‘WebContent/WEB-INF/lib/xxxxx.jar cannot ...