plzdontkillme
plzdontkillme

Reputation: 1527

Storing Android Emoji in MySQL

We are storing the emoji selected by the users in text body in MySQL. We have the column looks like this

`body` text CHARACTER SET utf8 COLLATE utf8_bin,

Original data : <div id='mobile-question-style' style=\"font-family: 'Helvetica Neue',Helvetica,Arial; color:#333333;\">\uD83D\uDE1D\uD83D\uDE3E\uD83D\uDE3E\uD83D\uDE3A\uD83D\uDE3A\uD83D\uDE1D</div>

Data in DB : <div id='mobile-question-style' style="font-family: 'Helvetica Neue',Helvetica,Arial; color:#333333;">????????????</div>

But when if fetch it from the Rest API it looks correct : "body":"<div id='mobile-question-style' style=\"font-family: 'Helvetica Neue',Helvetica,Arial; color:#333333;\">\uD83D\uDE1D\uD83D\uDE3E\uD83D\uDE3E\uD83D\uDE3A\uD83D\uDE3A\uD83D\uDE1D</div>",

Now when I update the question, using Rest API it looses it encoding and showes "????"

Data from endpoint after I update : "body":"<div id='mobile-question-style' style=\"font-family: 'Helvetica Neue',Helvetica,Arial; color:#333333;\">????????????</div>",

There is special logic when updating the body so does any one knows what is going on here?

Upvotes: 1

Views: 4133

Answers (1)

superuserdo
superuserdo

Reputation: 1657

To store emojis into a Mysql Database convert the string you are storing into an encoded Base 64 string. On your application side all you have to do is decode that string using Base 64.

Encoding

                byte[] data = editTextFieldWithEmojis.getText().toString().getBytes("UTF-8");
                base64String = Base64.encodeToString(data, Base64.DEFAULT);

Decoding

                byte[] data = Base64.decode(stringWithEmojis, Base64.DEFAULT);
                newStringWithEmojis = new String(data, "UTF-8");

Upvotes: 7

Related Questions