android source

bitmap <-> string

리오파파 2023. 12. 17. 20:05

GSON으로 bitmap을 보관하기 위한 conversion

 


public static String bitmap2string(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream); // Adjust quality as needed
byte[] bitmapBytes = outputStream.toByteArray();
return Base64.encodeToString(bitmapBytes, Base64.NO_WRAP);
}

public static Bitmap string2bitmap (String base64String) {
byte[] decodedBytes = Base64.decode(base64String, Base64.NO_WRAP);
return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
}

 

'android source ' 카테고리의 다른 글

phone vibrate  (0) 2023.12.17
Phone Metric 얻기  (0) 2023.12.17
layer xml로 버튼모양 만들기  (0) 2023.12.17
Background Handler 준비해 두기  (0) 2022.05.15
Direction Sensor  (0) 2022.05.15