SDCard助手类

时间:2021-11-14 09:06:29
 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