android 文件保存
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
public class FileCache {
// Context mContext;
static File fileDir;
// data/data+包名+images/hashcode
// private static final String SDCARD_ABSOLUTION = "/mnt/emmc/" +
// "/duopinImage/";/*
// * sdcard
// * map
// * to
// * emmc
// */
//
private static final String TAG = "FileCache";
private static String SAVE_PATH = null;// "com.duopin.gifdemo";
private static final String DISK_CACHE_SUBDIR = "imageCache";
private static String SDCARD_ABSOLUTION = null;// "/data/data/" + SAVE_PATH
// + File.separator +
// DISK_CACHE_SUBDIR;
// private static final String SDCARD_ABSOLUTION = "/data/data/" +
// context.getCacheDir().getPath(); + File.separator + DISK_CACHE_SUBDIR;
public static File getFileDir() {
return fileDir;
}
public static boolean isHasSdCard() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return true;
}
return false;
}
// pkg =getContext().getCacheDir().getPath()
public static boolean setupImagesDir(String dirPath) {
// path=====/data/data/com.duopin.app/cache (data 分区,not sdCard)
Log.e("FileCache", "setupImagesDir.........dirPath:" + dirPath);
if (null == dirPath) {
Log.e("FileCache", "setupImagesDir.........failure.....please input package para..........");
return false;
}
SAVE_PATH = dirPath;
SDCARD_ABSOLUTION = SAVE_PATH + File.separator + DISK_CACHE_SUBDIR;
if (FileCache.isHasSdCard()) {
if (fileDir == null) {
fileDir = new File(SDCARD_ABSOLUTION);
if (!fileDir.exists()) {
Log.e("FileCache", "fileDir.mkdir()........................");
fileDir.mkdir();
return true;
} else {
Log.e("FileCache", "fileDir.exists........................");
return true;
}
} else {
Log.e("FileCache", "fileDir not null........................");
return false;
}
}
return false;
}
public synchronized static boolean saveImageToFile(Bitmap bitmap, String filePath) throws IOException {
// File saveFile = new File(fileDir, getFileName(filePath) +
// ".jpg");/* 用JPG保存图片 */
/* 按扩展名的类型决定MimeType */
File saveFile = null;
if (filePath.contains("/")) {
saveFile = new File(fileDir, getFileName(filePath) + filePath.substring(filePath.lastIndexOf(".")));// fileName
} else {
saveFile = new File(fileDir, filePath + ".jpg");// fileName
}
// and
// 扩展名
try {
FileOutputStream fos = new FileOutputStream(saveFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);/*
* 80代表是原图的质量的80%
* ,
* 但width,height
* 不变
*/
bos.flush();
bos.close();
Log.d("memory9", "saveImageToFile........................success filePath:" + filePath);
return true;
} catch (FileNotFoundException e) {
Log.e("FileCache", "FileNotFoundException........................");
return false;
}
}
/*
* 获得文件名字,包括后缀
*/
public static String getFileNameAndHouzui(String filePath) {
int startIndex = filePath.lastIndexOf("/");
String fileName = filePath.substring(startIndex + 1);
return fileName;
}
/*
* 获得文件名字,不包括后缀
*/
public static String getFileName(String filePath) {
int startIndex = filePath.lastIndexOf("/");
int endIndex = filePath.lastIndexOf(".");
String fileName = filePath.substring(startIndex + 1, endIndex);
return fileName;
}
public static Bitmap getImageFromFile(final String filePath) {
if (filePath == null) {
Log.d("FileCache", "imageUri is null........................");
return null;
}
// String FileRootDir = getImageFileAbsolution();
String imageName = null;
String imageAbsolutionDir = null;
if (filePath.contains("/")) {
imageName = filePath.substring(filePath.lastIndexOf("/"));// 还有扩展名
imageAbsolutionDir = SDCARD_ABSOLUTION + imageName;
Log.d("memory8", "imageUri is null........................imageAbsolutionDir->" + imageAbsolutionDir);
} else {
imageName = filePath + ".jpg";
imageAbsolutionDir = SDCARD_ABSOLUTION + File.separator + imageName;
Log.d("memory8", "imageUri is null........................imageAbsolutionDir->" + imageAbsolutionDir);
}
Bitmap bitmap = BitmapFactory.decodeFile(imageAbsolutionDir);// ???
if (bitmap != null) {
Log.d("memory888", "imageUri is null........................bitmap is not null");
}
return bitmap;
}
public static String getImageFileAbsolution() {
// return Environment.getExternalStorageDirectory();
return SDCARD_ABSOLUTION;
}
/* no use */
public static void writeToFile(InputStream is, OutputStream os) throws IOException {
// TODO Auto-generated method stub
final int buffer_ByteSize = 1024;
byte[] buffer = new byte[buffer_ByteSize];
int hasReadByte = 0;
try {
while ((hasReadByte = is.read(buffer)) > 0) {
os.write(buffer, 0, hasReadByte);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("", "writeToFile catch Exception...");
}
}
/**
* @Title: deleteFile
* @Description:
* @param :if null then delete save dir
* @return boolean
* @throws
*/
public static boolean deleteFile(String filePath) {
if (filePath == null) {
filePath = SAVE_PATH;
}
Log.e("FileCache", "Delete filePath:" + filePath);
// File file = new File(getImageFileAbsolution());
File file = new File(filePath);
if (file == null || !file.exists()) {
return false;
}
if (file.isFile()) {
file.delete();
} else if (file.isDirectory()) {
File[] fileList = file.listFiles();
for (int i = 0; i < fileList.length; i++) {
Log.e("memory", "Delete File:" + fileList[i].getAbsolutePath());
Log.e("memory8", "Delete File:" + fileList[i].getAbsolutePath());
Log.e("FileCache", "Delete File:" + fileList[i].getAbsolutePath());
if (fileList[i].isDirectory()) {
deleteFile(fileList[i].getAbsolutePath());// 递归目录
} else {
fileList[i].delete();
}
}
}
/* 最后目录是文件了,就可以删除 */
Log.e("FileCache", "Delete File:" + file.getAbsolutePath());
Log.e("memory", "Delete File:" + file.getAbsolutePath());
Log.e("memory8", "Delete File:" + file.getAbsolutePath());
file.delete();
fileDir = null;// must add,next creat dir
return true;
}
}