还是最近整理代码、、发现了一个师兄写的图片工具类,感觉还是蛮有用的、、主要是图片上传和下载用的比较多吧/*
* version date author
* ──────────────────────────────────
* 1.0 2010-9-17 Neal Miao
*/
package com.winfar.ic.util;
import java.io.BufferedInputStream;
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 org.kobjects.base64.Base64;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
/**
* 图片处理工具类
*
* @author Neal Miao
* @version
* @since v 1.0
* @Date 2011 May 24, 2011 7:24:50 PM
*
* @see
*/
public class ImageUtil {
/**
* 从SD卡里面读取图片
*
* @param fileName
* @return
* @return Bitmap
* @since v 1.0
*/
public static Bitmap getBitmapByPath(String fileName) {
String myJpgPath = "/sdcard/ic_tmp/" + fileName;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 12;
Bitmap bm = BitmapFactory.decodeFile(myJpgPath, options);
return bm;
}
/**
* 保存图片到SD卡
*
* @param bitName
* @param mBitmap
* @throws IOException
* @return void
* @since v 1.0
*/
public static void saveMyBitmap(String bitName, Bitmap mBitmap)
throws IOException {
File tmp = new File("/sdcard/ic_tmp/");
if (!tmp.exists()) {
tmp.mkdir();
}
File f = new File("/sdcard/ic_tmp/" + bitName + ".png");
f.createNewFile();
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
try {
fOut.flush();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String image2Bytes(String imageName) {
File tmp = new File("/sdcard/ic_tmp/");
if (!tmp.exists()) {
return "";
}
int bufferSize = 1024;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
File f = new File("/sdcard/ic_tmp/" + imageName);
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
int ch;
int i = 0;
while ((ch = bis.read()) != -1) {
baos.write(ch);
if (i++ == bufferSize) {
baos.flush();
i = 0;
}
}
baos.flush(); // 提交文件流,很关键
bis.close();
} catch (FileNotFoundException e) {
MyLog.e("ImageUtil", e.getMessage());
return "";
} catch (IOException e) {
MyLog.e("ImageUtil", e.getMessage());
return "";
}
return Base64.encode(baos.toByteArray());
}
}