http://blog.csdn.net/z104207/article/details/6634774
[java] view
plaincopy
package com.bingo.util;
import java.io.bufferedoutputstream;
import java.io.bytearrayoutputstream;
import java.io.file;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import android.graphics.bitmap;
import android.graphics.bitmapfactory;
import android.graphics.matrix;
public class imagedispose {
/**
* @param 将圖檔内容解析成位元組數組
* @param instream
* @return byte[]
* @throws exception
*/
public static byte[] readstream(inputstream instream) throws exception {
byte[] buffer = new byte[1024];
int len = -1;
bytearrayoutputstream outstream = new bytearrayoutputstream();
while ((len = instream.read(buffer)) != -1) {
outstream.write(buffer, 0, len);
}
byte[] data = outstream.tobytearray();
outstream.close();
instream.close();
return data;
}
* @param 将位元組數組轉換為imageview可調用的bitmap對象
* @param bytes
* @param opts
* @return bitmap
public static bitmap getpicfrombytes(byte[] bytes,
bitmapfactory.options opts) {
if (bytes != null)
if (opts != null)
return bitmapfactory.decodebytearray(bytes, 0, bytes.length,
opts);
else
return bitmapfactory.decodebytearray(bytes, 0, bytes.length);
return null;
* @param 圖檔縮放
* @param bitmap 對象
* @param w 要縮放的寬度
* @param h 要縮放的高度
* @return newbmp 新 bitmap對象
public static bitmap zoombitmap(bitmap bitmap, int w, int h){
int width = bitmap.getwidth();
int height = bitmap.getheight();
matrix matrix = new matrix();
float scalewidth = ((float) w / width);
float scaleheight = ((float) h / height);
matrix.postscale(scalewidth, scaleheight);
bitmap newbmp = bitmap.createbitmap(bitmap, 0, 0, width, height,
matrix, true);
return newbmp;
* 把bitmap轉byte
* @author heh
* @edittime 2010-07-19 上午11:45:56
public static byte[] bitmap2bytes(bitmap bm){
bytearrayoutputstream baos = new bytearrayoutputstream();
bm.compress(bitmap.compressformat.png, 100, baos);
return baos.tobytearray();
* 把位元組數組儲存為一個檔案
public static file getfilefrombytes(byte[] b, string outputfile) {
bufferedoutputstream stream = null;
file file = null;
try {
file = new file(outputfile);
fileoutputstream fstream = new fileoutputstream(file);
stream = new bufferedoutputstream(fstream);
stream.write(b);
} catch (exception e) {
e.printstacktrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (ioexception e1) {
e1.printstacktrace();
}
}
return file;
}