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. Sicily-1050 深度优先搜索

    一.      题意 给出5个数和4则运算,看能不能算出目标值出来,如果算不出来就算出比目标值小的最大值.深搜:每一步选两个数做运算,然后算出的结果作为下一步的其中一个操作数.每一步选数有C(5,2) ...

  2. linux下笔记本有线网卡"未受管理"

    前段时间因为在弄一个笔记双网卡共享上网的事情把笔记本的有线网卡弄环了,连接的时候一直出现如下情况: 1)有线网卡:未受管理 2)无线网卡:每次登录的时候必须把原来登录过的信息删除掉,然后重新输入密码, ...

  3. Ext JS学习第十六天 事件机制event(一)

    此文用来记录学习笔记: 休息了好几天,从今天开始继续保持更新,鞭策自己学习 今天我们来说一说什么是事件,对于事件,相信你一定不陌生, 基本事件是什么?就类似于click.keypress.focus. ...

  4. c 输入两个数,第一个数决定一个nXn的矩阵,第二个数决定从1开始赋值,赋值的上限 (MD花了半天时间,思路不对害死人)

    输入两个数,第一个数决定一个nXn的矩阵,第二个数决定从1开始赋值,赋值的上限 比如: 输入: 输出: 输入: 输出: #include<stdio.h> int main(void) { ...

  5. 由命名空间函数而引发思考--js中的对象赋值问题

    最近没有编码任务,作为一个才毕业的小辣鸡,给的任务就是看一下公司的新系统,熟悉怎么用哪些地方是干什么的. 下午喝了两杯水,感觉有点浪.然后就开始看了下代码.发现有一个函数是这样子的. var TX = ...

  6. LeetCode 二叉树后序遍历(binary-tree-postorder-traversal)

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  7. CButtonST的用法详解【转】

    在想使用CButtonST的工程中加入BtnST.h.BtnST.cpp.BCMenu.h.BCMenu.cpp4个文件.2个类. 1. 在按钮上加入Icon,使Icon和文字同时显示 假设按钮ID为 ...

  8. 基本的编程原则SOLID

    1.单一职责原则:每个类只负责完成一个职责,当它需要完成多个职责时就需要将它拆分开来. 2.开放封闭原则:对扩展开放,对修改封闭. 3.里氏替换原则:子类对象可以替换(代替)它的所有父类(超类)对象. ...

  9. [置顶] 关于CSDN2013博客之星的一些看法

    最近一个周,最火的话题当然要数CSDN2013博客之星拉票了. 实话实说,从12月14日开始,我连续5天拉票. 通过QQ群.QQ好友.CSDN文章.给CSDN粉丝发私信等多种方式拉票,真是累死我了. ...

  10. JavaScript基础知识----document对象

    对象属性document.title                 //设置文档标题等价于HTML的<title>标签document.bgColor               //设 ...