Skip to content

Instantly share code, notes, and snippets.

@brandhill
Created February 3, 2015 09:08
Show Gist options
  • Save brandhill/6587d02bd88b95b44668 to your computer and use it in GitHub Desktop.
Save brandhill/6587d02bd88b95b44668 to your computer and use it in GitHub Desktop.
Android 的 Bitmap 轉成 Base64 並存入 SQLite 資料庫的方法
//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