近來要做個項(xiàng)目,里邊要求有emoji表情列表,“表情”在很多即時聊天IM的項(xiàng)目中會用到垦页,很多存儲的是圖片掌唾,而emoji表情是通過utf/Unicode編碼進(jìn)行顯示的放前。我使用的是Unicode編碼,通過自己創(chuàng)建了一個emoji的Unicode編碼的數(shù)據(jù)庫糯彬,在工程中使用凭语。
emoji各種表情編碼的網(wǎng)址:http://unicode.org/emoji/charts/full-emoji-list.html#1f600
自己創(chuàng)建的一個部分emoji表情的sqlite數(shù)據(jù)庫:http://download.csdn.net/detail/went_gone/9666565
下載下來后放在自己項(xiàng)目的assets文件夾下,在代碼中將assets下的此數(shù)據(jù)庫讀寫到database下進(jìn)行數(shù)據(jù)庫操作撩扒。
使用SQLiteDatabase進(jìn)行操作數(shù)據(jù)庫時似扔,要傳遞數(shù)據(jù)庫所在的路徑。
/**
* 將assets目錄下的文件拷貝到database中
* @return 存儲數(shù)據(jù)庫的地址
*/
public static String CopySqliteFileFromRawToDatabases(String SqliteFileName) throws IOException {
// 第一次運(yùn)行應(yīng)用程序時搓谆,加載數(shù)據(jù)庫到data/data/當(dāng)前包的名稱/database/<db_name>
File dir = new File("data/data/" + BaseApplication.getContext().getPackageName() + "/databases");
if (!dir.exists() || !dir.isDirectory()) {
dir.mkdir();
}
File file= new File(dir, SqliteFileName);
InputStream inputStream = null;
OutputStream outputStream =null;
//通過IO流的方式炒辉,將assets目錄下的數(shù)據(jù)庫文件,寫入到應(yīng)用內(nèi)存中中挽拔。
if (!file.exists()) {
try {
file.createNewFile();
inputStream = BaseApplication.getContext().getClass().getClassLoader().getResourceAsStream("assets/" + SqliteFileName);
outputStream = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len ;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
if (inputStream != null) {
inputStream.close();
}
}
}
return file.getPath();
}
我們拿到了拷貝到手機(jī)上的emoji.db的路徑之后辆脸,就可以進(jìn)行數(shù)據(jù)庫的操作獲取emoji表情了。將emoji表情創(chuàng)建一個實(shí)體類EmojiBean用來管理螃诅。
public class EmojiBean {
private int id;
private int unicodeInt;
public String getEmojiString() {
return ExpressionUtil.getEmojiStringByUnicode(unicodeInt);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUnicodeInt() {
return unicodeInt;
}
public void setUnicodeInt(int unicodeInt) {
this.unicodeInt = unicodeInt;
}
}
還要將一點(diǎn)啡氢,將unicode轉(zhuǎn)化成emoji表情需要以下操作
public class ExpressionUtil {
/**
* 將unicode轉(zhuǎn)化成String
* @param unicode
* @return
*/
public static String getEmojiStringByUnicode(int unicode){
return new String(Character.toChars(unicode));
}
}
通過對數(shù)據(jù)庫的操作拿到了emoji的表情集合状囱,接下來就在工程中使用就可以了。
demo地址:https://github.com/WentGone/ApplicationEmoji
里邊有所有代碼倘是。