我正在制作一个聊天应用程序。我有一个用户个人资料的图像视图。
如果用户图像为空/空,我想显示用户名的第一个字母,例如:假设用户名是“未知”,所以我想在该图像视图上只显示 U。
点击这里查看我想要的样本
我试过了 :
首先,我检查了用户图像是否为空
if (item.getSender_img_url() == null || item.getSender_img_url().isEmpty()||item.getSender_img_url().equals("null") ) {
System.out.println(TAG + " photo is not available ");
//here i am getting first alphabet of Users Name
String[] result = item.getSenderName().split("\\s+");
// This is a regex for matching spaces
// The string we'll create
String abbrev = "";
// Loop over the results from the string splitting
for (int i = 0; i < result.length; i++) {
System.out.println(TAG + " abbrev 1 "+ abbrev);
// Grab the first character of this entry
char c = result[i].charAt(0);
System.out.println(TAG + " abbrev c "+ c);
// If its a number, add the whole number
if (c >= '0' && c <= '9') {
abbrev += result[i];
}
// If its not a number, just append the character
else {
abbrev += c;
}
System.out.println(TAG + " abbrev 3 "+ abbrev);
}
//here i am converting string value into image
Bitmap bm = StringToBitMap(abbrev.toString());
//here i am setting covertes bitmat image into image view
((ViewHolder) holder).friends_image.setImageBitmap(bm);
} else {
System.out.println(TAG + " photo is available ");
try {
Picasso.with(mContext).load(item.getSender_img_url())
.error(R.drawable.profile_avatar)
.placeholder(R.drawable.profile_avatar).resize(40, 40).centerCrop()
.into(((ViewHolder) holder).friends_image);
} catch (Exception e) {
e.printStackTrace();
}
}
.................................
/**
* @param encodedString
* @return bitmap (from given string)
*/
public static Bitmap StringToBitMap(String encodedString){
try {
byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
} catch(Exception e) {
e.getMessage();
return null;
}
}
有什么帮助吗?