您的位置:首页 > 移动开发 > Android开发

Android 本地文件管理类

2016-03-07 00:00 411 查看
摘要: Android本地文件管理类:

Android本地文件管理类:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

public class FileUtil {

private static final String TAG = "FileUtil";
private Context context;
private String SRCPATH;
private String THUMBPATH;
private boolean hasSD = false;

/**
* 单例模式
*/
public FileUtil(Context context) {
this.context = context;
// 判断是否含有SD卡
hasSD = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
String rootPath = hasSD ? (Environment.getExternalStorageDirectory().getPath() + File.separator + "Android" + File.separator + "data" + File.separator + SwitchAppTool.APPLICATION_ID + File.separator + "cache" + File.separator) : (this.context.getFilesDir().getPath() + File.separator + "Android" + File.separator + "data" + File.separator + SwitchAppTool.APPLICATION_ID + File.separator + "cache" + File.separator);
THUMBPATH = rootPath + "thumb" + File.separator;
SRCPATH = rootPath + "src" + File.separator;
File src = new File(SRCPATH), thumb = new File(THUMBPATH);
if (!src.exists()) {
src.mkdirs();
}
Log.e(TAG, src.getPath());
if (!thumb.exists()) {
thumb.mkdirs();
}
Log.e(TAG, thumb.getPath());
}

private static FileUtil instance;

public static FileUtil getInstance(Context context) {
if (instance == null) {
instance = new FileUtil(context);
}
return instance;
}

/**
* 写文件到sdcard
*
* @param fileName
* @param bm
* @param isThumb
* @return
*/
public boolean writeFileToSD(String fileName, Bitmap bm, boolean isThumb) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);// png类型
byte[] datas = baos.toByteArray();
return writeFileToSD(fileName, datas, isThumb);
}

/**
* 将文件写入SD卡
*/
public boolean writeFileToSD(String fileName, byte[] datas, boolean isThumb) {
try {
File file = new File((isThumb ? THUMBPATH : SRCPATH) + fileName);
Log.e(TAG, "writeFileToSD[]:" + file.getPath());
if (!file.exists()) {
file.createNewFile();// 创建文件
}
FileOutputStream stream = new FileOutputStream(file);
stream.write(datas);
stream.close();

return true;
} catch (Exception e) {
Log.e(TAG, "writeFileToSD[]:Error" + e.getMessage());
}
return false;
}

/**
* 判断sdcard有没有文件或文件夹
*
* @param fileName
* @return
*/
public boolean existsSrc(String fileName) {
boolean flag = new File(SRCPATH + fileName).exists();
Log.e(TAG, "existsSrc[]:" + SRCPATH + fileName + ";result:" + flag);
return flag;
}

private static CustomProgress customProgress = null;

public boolean clear(Context context) {
customProgress = CustomProgress.show(context, "loading...", false, null);
File srcDir = new File(SRCPATH);

String[] children = srcDir.list();
for (int i = 0; i < children.length; i++) {
if (!new File(srcDir, children[i]).delete()) {
return false;
}
}
File thumbDir = new File(THUMBPATH);
children = thumbDir.list();
for (int i = 0; i < children.length; i++) {
if (!new File(thumbDir, children[i]).delete()) {
return false;
}
}
customProgress.dismiss();
return true;
}

public boolean existsThumb(String fileName) {
boolean flag = new File(THUMBPATH + fileName).exists();
Log.e(TAG, "existsThumb[]:" + THUMBPATH + fileName + ";result:" + flag);
return flag;
}

/**
* 读取文件
*/
private Bitmap readSDFile(String fileName, boolean isThumb) {
String pathName = (isThumb ? THUMBPATH : SRCPATH) + fileName;
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// 获取这个图片的宽和高,注意此处的bitmap为null
bitmap = BitmapFactory.decodeFile(pathName, options);
options.inJustDecodeBounds = false; // 设为 false

// 重新读入图片,读取缩放后的bitmap,注意这次要把options.inJustDecodeBounds 设为 false
bitmap = BitmapFactory.decodeFile(pathName, options);
// 利用ThumbnailUtils来创建缩略图,这里要指定要缩放哪个Bitmap对象

return bitmap;
}

public Bitmap readSrcFile(String fileName) {
return readSDFile(fileName, false);
}

public Bitmap readThumbFile(String fileName) {
return readSDFile(fileName, true);
}

public String getSRCPATH() {
return SRCPATH;
}

public String getTHUMBPATH() {
return THUMBPATH;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息