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. Scrambled Polygon(凸多边形,斜率)

    Scrambled Polygon Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7805   Accepted: 3712 ...

  2. POJ 2695 The Pilots Brothers' refrigerator(神奇的规律)

    转载请注明出处:http://blog.csdn.net/a1dark 分析:如果想要将一个“+”翻转成“-”,那么必然会把对应的行和列上的所有点翻转一次.由于一个点翻偶数次就相当于不翻转.所以我需要 ...

  3. android项目 之 记事本(6)----- 加入手写

    想必大家都用过QQ的白板功能,里面主要有两项,一个是涂鸦功能,事实上类似于上节的画板功能,而还有一个就是手写,那记事本怎么能没有这个功能呢,今天就来为我们的记事本加入手写功能. 先上图,看看效果: 看 ...

  4. JavaBean的一个小例子

    一.创建一个javaBean类: UseBean package com.oncall24h.ruchi; import java.io.Serializable; public class UseB ...

  5. HTML5 总结-Web存储-7

    HTML 5 Web 存储 在客户端存储数据 HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没有时间限制的数据存储 sessionStorage - 针对一个 ses ...

  6. [LeetCode]题解(python):018-4Sum

    题目来源: https://leetcode.com/problems/4sum/ 题意分析: 这道题目和3Sum的题目类似,找出所有的4个数,使得这4个数等于target. 题目思路: 这道题做法和 ...

  7. The reference to entity "characterEncoding" must end with the ';' delimiter

    数据源配置时加上编码转换格式后出问题了: The reference to entity "characterEncoding" must end with the ';' del ...

  8. Girls' research(manacher)

    Girls' research Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) ...

  9. 【转】Android数字证书

    Android数字证书的作用是非常重要的.Android操作系统每一个应用程序的安装都需要经过这一数字证书的签名. Android手机操作系统作为一款比较流行的开源系统在手机领域占据着举足轻重的地位. ...

  10. 聊聊高并发(二十五)解析java.util.concurrent各个组件(七) 理解Semaphore

    前几篇分析了一下AQS的原理和实现.这篇拿Semaphore信号量做样例看看AQS实际是怎样使用的. Semaphore表示了一种能够同一时候有多个线程进入临界区的同步器,它维护了一个状态表示可用的票 ...