package com.lt.an20_utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.StatFs;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by 风情万种冷哥哥 on 2016/10/15.
*/
public class SDCardUtils {
//判断sd卡是否被挂载
public static boolean isSDCardMounted(){
return Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);
}
/**
* Created by 风情万种冷哥哥 on 2016.
* 获取sdcard常用目录
*/
public static String getSDCardBaseDir(){
if (isSDCardMounted()){
return Environment.getExternalStorageDirectory().getAbsolutePath();
}
return null;
}
//获取sdcard公有的目录的路径
public static String getSDCardPublicDir(String type){
if(isSDCardMounted()){
return Environment.getExternalStoragePublicDirectory(type).toString();
}
return null;
}
//获取sdcard私有cache的目录的路径
public static String getSDCardPrivateCacheDir(Context context){
if (isSDCardMounted()){
return context.getExternalCacheDir().getAbsolutePath();
}
return null;
}
//获取sdcard私有file目录的路径
public static String getSDCardPrivateFilesDir(Context context,String type){
if (isSDCardMounted()){
return context.getExternalFilesDir(type).getAbsolutePath();
}
return null;
}
/**
* Created by 风情万种冷哥哥 on 2016.
* 获取sdcard空间的大小
*/
//获取sdcard的完整空间大小 。返回MB
public static long getSDCardSize(){
if (isSDCardMounted()){
StatFs fs = new StatFs(getSDCardBaseDir());
int count = fs.getBlockCount();
int size = fs.getBlockSize();//此处过时了但也没有更好的方法更新
return count*size/1024/1024;
}
return 0;
}
//获取sdcard的剩余空间的大小
public static long getSDCardFreeSize(){
if (isSDCardMounted()){
StatFs fs = new StatFs(getSDCardBaseDir());
int count = fs.getFreeBlocks();
int size = fs.getBlockSize();
return count*size/1024/1024;
}
return 0;
}
//获取sdcard的可用空间的大小
public static long getSDCardAvailableSize(){
if (isSDCardMounted()){
StatFs fs = new StatFs(getSDCardBaseDir());
int count = fs.getAvailableBlocks();
int size = fs.getBlockSize();
return count*size/1024/1024;
}
return 0;
}
/**
* Created by 风情万种冷哥哥 on 2016.
* 往sdcard的公有目录下保存文件
*
*
*/
public static boolean saveFileToSDCardPublicDir(byte[] data,String type,String fileName){
BufferedOutputStream bos = null;
if (isSDCardMounted()){
File file = Environment.getExternalStoragePublicDirectory(type);
try {
bos = new BufferedOutputStream(new FileOutputStream(new File(file,fileName)));
bos.write(data);
bos.flush();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return false;
}
//往sdcard的自定义目录下保存文件
public static boolean saveFileToSDCardCustomDir(byte[] data,String dir,String fileName){
BufferedOutputStream bos = null;
if (isSDCardMounted()){
File file = new File(getSDCardBaseDir()+File.separator+dir);
if (!file.exists()){
file.mkdirs();//递归创建自定义目录
try {
bos = new BufferedOutputStream(new FileOutputStream(new File(file,fileName)));
bos.write(data);
bos.flush();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
return false;
}
//往sdcard的私有files目录下保存文件
public static boolean saveFlieToSDCardPrivateFileDir(byte[] data,String type,String fileName,Context context){
BufferedOutputStream bos = null;
if (isSDCardMounted()){
File file = context.getExternalFilesDir(type);
try {
bos = new BufferedOutputStream(new FileOutputStream(new File(file,fileName)));
bos.write(data);
bos.flush();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(bos != null){
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
//往sdcard的私有cache目录下保存文件
public static boolean saveFileToSDCardPrivateCacheDir(byte[] data,String fileName,Context context){
BufferedOutputStream bos = null;
if (isSDCardMounted()){
File file = context.getExternalCacheDir();
try {
bos = new BufferedOutputStream(new FileOutputStream(new File(file,fileName)));
bos.write(data);
bos.flush();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return false;
}
//保存bitmap图片到sdcard的私有目录
public static boolean saveBitmapToSDCardCacheDir(Bitmap bitmap,String fileName,Context context){
if (isSDCardMounted()){
BufferedOutputStream bos = null;
//获取私有的cache的缓存目录
File file = context.getExternalCacheDir();
try {
bos = new BufferedOutputStream(new FileOutputStream(new File(file,fileName)));
if (fileName != null && (fileName.contains(".png") || fileName.contains(".PNG"))){
bitmap.compress(Bitmap.CompressFormat.PNG,100,bos);
}else {
bitmap.compress(Bitmap.CompressFormat.JPEG,100,bos);
}
bos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}else {
return false;
}
}
//将图片保存到sdcard公有目录
public static boolean saveBitmapToSDCardPublicDir(Bitmap bm,String type,String fileName){
if (isSDCardMounted()){
String filepath = getSDCardPublicDir(type)+File.separator+fileName;
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(new File(filepath)));
bm.compress(Bitmap.CompressFormat.PNG,100,bos);
bos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
}
return false;
}
// 从SD卡获取文件
public static byte[] loadFileFromSDCard(String fileDir) {
BufferedInputStream bis = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
bis = new BufferedInputStream(
new FileInputStream(new File(fileDir)));
byte[] buffer = new byte[8 * 1024];
int c = 0;
while ((c = bis.read(buffer)) != -1) {
baos.write(buffer, 0, c);
baos.flush();
}
return baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (baos != null) {
baos.close();
}
if (bis != null) {
bis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
// 从SDCard中寻找指定目录下的文件,返回Bitmap
public Bitmap loadBitmapFromSDCard(String filePath) {
byte[] data = loadFileFromSDCard(filePath);
if (data != null) {
Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
if (bm != null) {
return bm;
}
}
return null;
}
public static boolean isFileExist(String filePath) {
File file = new File(filePath);
return file.isFile();
}
// 从sdcard中删除文件
public static boolean removeFileFromSDCard(String filePath) {
File file = new File(filePath);
if (file.exists()) {
try {
file.delete();
return true;
} catch (Exception e) {
return false;
}
} else {
return false;
}
}
/**
* Created by 风情万种冷哥哥 on 2016.
* 输入流转字节数组
*/
public static byte[] streamToByteArray(InputStream is) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c = 0;
byte[] buffer = new byte[8 * 1024];
try {
while ((c = is.read(buffer)) != -1) {
baos.write(buffer, 0, c);
baos.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return baos.toByteArray();
}
/**
* Created by 风情万种冷哥哥 on 2016.
* * @return
*/
public static String streamToString(InputStream is, String charsetName) {
BufferedInputStream bis = new BufferedInputStream(is);
StringBuilder sb = new StringBuilder();
int c = 0;
byte[] buffer = new byte[8 * 1024];
try {
while ((c = bis.read(buffer)) != -1) {
sb.append(new String(buffer, charsetName));
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* Created by 风情万种冷哥哥 on 2016.
* 字符串转输入流
*/
public static InputStream stringToInputStream(String str) {
InputStream is = null;
try {
is = new ByteArrayInputStream(str.getBytes("UTF-8"));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return is;
}
}
作者:First_CooMan 发表于2016/10/18 12:58:49 原文链接
阅读:28 评论:0 查看评论