1、将Bitmap对象读到字节数组中
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] datas = baos.toByteArray();
2、将字节数组转为Bitmap对象
byte[] b = getIntent().getByteArrayExtra("bitmap");
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
3、图片文件转为Bitmap对象
String filePath=”c:/01.jpg”;
Bitmap bitmap=BitmapFactory.decodeFile(filePath);
注:
如果图片过大,可能导致Bitmap对象装不下图片
解决办法:
String filePath=”c:/01.jpg”;
Bitmap bitmap=BitmapFactory.decodeFile(filePath,getBitmapOption(2)); //将图片的长和宽缩小味原来的1/2
private Options getBitmapOption(int inSampleSize){
System.gc();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inSampleSize = inSampleSize;
return options;
}
4、Bitmap对象保存图片文件
public void saveBitmapFile(Bitmap bitmap){
File file=new File(“/mnt/sdcard/pic/01.jpg”);//将要保存图片的路径
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
5、bitmap和base64之间的转换
/**
* bitmap转为base64
* @param bitmap
* @return
*/
public static String bitmapToBase64(Bitmap bitmap) {
String result = null;
ByteArrayOutputStream baos = null;
try {
if (bitmap != null) {
baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
baos.flush();
baos.close();
byte[] bitmapBytes = baos.toByteArray();
result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (baos != null) {
baos.flush();
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* base64转为bitmap
* @param base64Data
* @return
*/
public static Bitmap base64ToBitmap(String base64Data) {
byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
6、网络图片Url 转 Bitmap(须在子线程)
public Bitmap getBitmap(String url) {
Bitmap bm = null;
try {
URL iconUrl = new URL(url);
URLConnection conn = iconUrl.openConnection();
HttpURLConnection http = (HttpURLConnection) conn;
int length = http.getContentLength();
conn.connect();
// 获得图像的字符流
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is, length);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();// 关闭流
}
catch (Exception e) {
e.printStackTrace();
}
return bm;
}
7、将view转为bitmap
//1.0
public static Bitmap getBitmapFromView(View view)
{
// Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
// Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
// Get the view's background
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
// has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
// does not have background drawable, then draw white background on
// the canvas
canvas.drawColor(Color.WHITE);
// draw the view on the canvas
view.draw(canvas);
// return the bitmap
return returnedBitmap;
}
//2.0
public static Bitmap viewToBitmap(View view)
{
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bm = view.getDrawingCache();
return bm;
}
8、图片转为文件
public static boolean saveBitmap2file(Bitmap bmp)
{
CompressFormat format = Bitmap.CompressFormat.PNG;
int quality = 100;
OutputStream stream = null;
try
{
// 判断SDcard状态
if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
{
// 错误提示
return false;
}
// 检查SDcard空间
File SDCardRoot = Environment.getExternalStorageDirectory();
if (SDCardRoot.getFreeSpace() < 10000)
{
// 弹出对话框提示用户空间不够
Log.e("Utils", "存储空间不够");
return false;
}
// 在SDcard创建文件夹及文件
File bitmapFile = new File(SDCardRoot.getPath() + FILE_PATH);
bitmapFile.getParentFile().mkdirs();// 创建文件夹
stream = new FileOutputStream(SDCardRoot.getPath() + FILE_PATH);// "/sdcard/"
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
return bmp.compress(format, quality, stream);
}