SDCard助手类
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助手类的更多相关文章
- ADO.NET数据库操作助手类
SQL语句操作增删查改助手类 using System; using System.Collections.Generic; using System.Configuration; using Sys ...
- 【C#】SQL数据库助手类2.0(自用)
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using ...
- AES加密解密 助手类 CBC加密模式
"; string result1 = AESHelper.AesEncrypt(str); string result2 = AESHelper.AesDecrypt(result1); ...
- Yii2 数组助手类arrayHelper
数组助手类 ArrayHelper 1.什么是数组助手类 Yii 数组助手类提供了额外的静态方法,让你更高效的处理数组. a.获取值(getValue) class User { public $na ...
- WorldWind源码剖析系列:代理助手类ProxyHelper
代理助手类ProxyHelper通过平台调用的互操作技术封送了若干Win32结构体和函数.该类类图如下. 提供的主要处理方法基本上都是静态函数,简要描述如下: 内嵌类型WINHTTP_AUTOPROX ...
- WorldWind源码剖析系列:图像助手类ImageHelper
图像助手类ImageHelper封装了对各种图像的操作.该类类图如下. 提供的主要处理方法基本上都是静态函数,简要描述如下: public static bool IsGdiSupportedImag ...
- php 数组助手类
ArrayHelper.php <?php /** * php 数组助手类 * Class ArrayHelper * @package app\helper */ class ArrayHel ...
- 数据库助手类 DBHelper
using System; using System.Collections.Generic; using System.Text; using System.Configuration; using ...
- Yii的数组助手类
获取值 用原生PHP从一个对象.数组.或者包含这两者的一个复杂数据结构中获取数据是非常繁琐的. 你首先得使用isset 检查 key 是否存在, 然后如果存在你就获取它,如果不存在, 则提供一个默认返 ...
随机推荐
- Tree(未解决。。。)
Tree Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- 获取WebView里的网页文本内容
获取WebView里的网页文本内容,能够採用例如以下方法: public class ComJSInterface { public void loadHtmlContent(String conte ...
- 软碟通UltraISO 9.65.3237官方注册版
UltraISO软碟通是一款功能强大.方便实用.老牌优秀的光盘映像文件制作/编辑/转换工具:可直接编辑ISO文件,从ISO中提取文件和目录:也可从CD – ROM制作光盘映像或者将硬盘上的文件制作成I ...
- jQuery也能舞出绚丽的界面(完结篇)
ThematicMap又增加了两种Chart类型,现在总算是齐全了,效果也出来了,与大家分享一下: 1.MultiSelect选择界面: 颜色框是可以选择颜色的: 2.生成的饼图效果: 3.生成的柱状 ...
- BZOJ 1631: [Usaco2007 Feb]Cow Party
题目 1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 491 Solved: 362[Sub ...
- [python]通过urllib2设置代理访问网址
#!/usr/bin/env pythonimport urllib2 # change followings before useuser = 'foo'passwd = 'bar'proxyser ...
- iOS_第3方类库_BlurAlertView_GPUImage
最终效果图: 先加入GPUImage.framework 导入BlurAlertView的类声明和类实现 // // BlurAlertView.h // 特效弹出框 // // Created by ...
- 平实给力的写作指导入门手冊——leo鉴书57
写作是个体力活儿,须要不断的练习和砥砺.既然是体力劳动,那必定有套路,前人总结.后人学习并加以积累沉积,日久则形成不同的风格和流派有点儿像.相同,写作也有自己的套路和学习路径.初涉写作有必备之书吗?当 ...
- eclipse+python+Anaconda ML环境搭建
整个流程按照这个来: http://blog.csdn.net/olaking/article/details/42435451 你会发现pydev安装不了,听说服务器被和谐了,下面这篇文章告诉你如何 ...
- light oj 1047-neighbor house
ime Limit:500MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Description The people ...