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);
	}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章