Android的Emoji表情😂

作者: Went_Gone | 来源:发表于2016-10-27 12:53 被阅读1123次

    近来要做个项目,里边要求有emoji表情列表,“表情”在很多即时聊天IM的项目中会用到,很多存储的是图片,而emoji表情是通过utf/Unicode编码进行显示的。我使用的是Unicode编码,通过自己创建了一个emoji的Unicode编码的数据库,在工程中使用。

    示例图

    emoji各种表情编码的网址:http://unicode.org/emoji/charts/full-emoji-list.html#1f600

    自己创建的一个部分emoji表情的sqlite数据库:http://download.csdn.net/detail/went_gone/9666565
    下载下来后放在自己项目的assets文件夹下,在代码中将assets下的此数据库读写到database下进行数据库操作。

    项目中emoji数据库

    使用SQLiteDatabase进行操作数据库时,要传递数据库所在的路径。

        /**
         * 将assets目录下的文件拷贝到database中
         * @return 存储数据库的地址
         */
        public static String CopySqliteFileFromRawToDatabases(String SqliteFileName) throws IOException {
            // 第一次运行应用程序时,加载数据库到data/data/当前包的名称/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目录下的数据库文件,写入到应用内存中中。
            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();
        }
    

    我们拿到了拷贝到手机上的emoji.db的路径之后,就可以进行数据库的操作获取emoji表情了。将emoji表情创建一个实体类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;
        }
    }
    

    还要将一点,将unicode转化成emoji表情需要以下操作

    public class ExpressionUtil {
        /**
         * 将unicode转化成String
         * @param unicode
         * @return
         */
        public static String getEmojiStringByUnicode(int unicode){
            return new String(Character.toChars(unicode));
        }
    }
    

    通过对数据库的操作拿到了emoji的表情集合,接下来就在工程中使用就可以了。

    demo地址:https://github.com/WentGone/ApplicationEmoji
    里边有所有代码。

    相关文章

      网友评论

      • 小欢_J:通俗易懂,很不错:relieved:
      • 938b0e0dfc52:怎么不能导入到工具中
        938b0e0dfc52: @Went_Gone 好吧,我承认我使用的工具错了,我用的ec
        Went_Gone: @蓝墨羽happy 你是说demo导不进去?你用的什么开发工具?这个是AndroidStudio的。
      • 包哥:留着以后学习,我是小白
        Went_Gone:@包哥 其实我也是小白的说... :blush:
      • 安静点就睡吧:写得不错:+1:
        Went_Gone:@veione 谢谢 :blush:

      本文标题:Android的Emoji表情😂

      本文链接:https://www.haomeiwen.com/subject/bbnjuttx.html