android 过滤字符串中的Emoji表情

时间:2023-01-11 16:02:18

在网上找了一部分,然后根据自己的项目进行了改动。
1.没有采用自定义的表情发送模式,而是直接套用的android系统里面的Emoji表情。
2.在表情发送的时候,也没有采用Unicode编码方式。因为项目原因,而是采用了如下简单的方式。
希望可以帮助大家,直接上使用的代码:

//提交之前的数据,使表情编程数字数组 [-16, -97, -104, -95]
String string;
public String filterEmoji(String source) {
if (!containsEmoji(source)) {
return source;// 如果不包含,直接返回
}
StringBuilder buf = null;
int len = source.length();
System.out.println("filter running len = " + len);
for (int i = 0; i < len; i++) {
char codePoint = source.charAt(i);
if (buf == null) {
buf = new StringBuilder(source.length());
}
if (!isEmojiCharacter(codePoint)) {
string = String.valueOf(codePoint);
} else {
try {
StringBuilder builder = new StringBuilder(2);
byte[] str = builder.append(String.valueOf(codePoint))
.append(String.valueOf(source.charAt(i+1)))
.toString().getBytes("UTF-8");
String strin = Arrays.toString(str);
String newString = strin.substring(1, strin.length() - 1);
string = "Γ"+newString+"Γ";
System.out.println("filters running newStr = " + string);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
i++;
}
buf.append(string+"⅞");
}
if (buf == null) {
return "";
} else {
if (buf.length() == len) {// 这里的意义在于尽可能少的toString,因为会重新生成字符串
buf = null;
return source;
} else {
System.out.println("filter running buf.toString() = " + buf.toString());
String bufStr = buf.toString();
String newBufStr= bufStr.substring(0, bufStr.length() - 1);
return newBufStr;
}
}
}

// 判别是否包含Emoji表情
private boolean containsEmoji(String str) {
int len = str.length();
for (int i = 0; i < len; i++) {
if (isEmojiCharacter(str.charAt(i))) {
return true;
}
}
return false;
}

private boolean isEmojiCharacter(char codePoint) {
return !((codePoint == 0x0) ||
(codePoint == 0x9) ||
(codePoint == 0xA) ||
(codePoint == 0xD) ||
((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) ||
((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)));
}

//得到服务器的数据之后进行解析,显示在UI上
String newsString;
public void setString(TextView textView,String string) {
StringBuilder stringBuilder = new StringBuilder();
String arrays[] = string.split("⅞");
for (int j = 0; j < arrays.length; j++) {
System.out.println("filter running arrays[] = "+arrays[j]);
String ss = arrays[j];
char char_ss = ss.charAt(0);
System.out.println("filter running String.valueOf(char_ss) = "+String.valueOf(char_ss));
if (String.valueOf(char_ss).equals("Γ")){
String new_SS = ss.substring(1, ss.length() - 1);
String strArrays[] = new_SS.split(", ");
byte[] chars = new byte[strArrays.length];
for (int i = 0; i < strArrays.length; ++i) {
System.out.println("strArrays[i]:" + strArrays[i]);
chars[i] = Byte.decode(strArrays[i]);
}
newsString = new String(chars);
}else{
newsString =ss;
}
stringBuilder.append(newsString);
System.out.println("filter running stringBuilder.toString() = "+stringBuilder.toString());
textView.setText(stringBuilder.toString());
}
}

更多资源源码下载:
不一样的RecyclerView优雅实现复杂列表布局
android自定义视频播放器
MediaPlayer和SurfaceView的结合使用
FloatingActionButton的使用
多层Fragment与ViewPager结合使用