Created
February 3, 2015 09:08
-
-
Save brandhill/6587d02bd88b95b44668 to your computer and use it in GitHub Desktop.
Android 的 Bitmap 轉成 Base64 並存入 SQLite 資料庫的方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Android的Bitmap轉成Base64並存入資料庫的方法 | |
// 先把 bitmpa 轉成 byte | |
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | |
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream ); | |
byte bytes[] = stream.toByteArray(); | |
// Android 2.2以上才有內建Base64,其他要自已找Libary或是用Blob存入SQLite | |
String base64 = Base64.encodeToString(bytes, Base64.DEFAULT); // 把byte變成base64 | |
// 再來是轉回來, 把Base64變回bytes | |
bytes = Base64.decode(base64, Base64.DEFAULT); | |
bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); //用BitmapFactory生成bitmap |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment