android下Bitmap和base64之间的转换

/*
	 * 将bitmap转换为base64字节数组
	 */
	public byte[] Bitmap2Base64(Bitmap bitmap) {
		try {
			// 先将bitmap转换为普通的字节数组
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
			out.flush();
			out.close();
			byte[] buffer = out.toByteArray();
			// 将普通字节数组转换为base64数组
			byte[] encode = Base64.decode(buffer, Base64.DEFAULT);
			return encode;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


	/*
	 * 将base64字节数组转换为bitmap
	 */
	public Bitmap Base642Bitmap(byte[] base64) {
		// 将base64字节数组转换为普通的字节数组
		byte[] bytes = Base64.decode(base64, Base64.DEFAULT);
		// 用BitmapFactory创建bitmap
		return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
	}


你可能感兴趣的:(android,base64)