채팅 응용 프로그램을 만들고 있습니다. 사용자 프로필에 대해 하나의 이미지 뷰가 있습니다.문자열 "A"를 안드로이드의 이미지로 변환하는 방법?
사용자 이미지가 null이거나 비어있는 경우 사용자 이름의 첫 번째 문자를 표시해야합니다. 예를 들어 사용자 이름이 '알 수 없음'으로 가정하고 해당 이미지보기에 U 만 표시하려고한다고 가정합니다.
Click here to see sample of what I want
나는 시도했다 : 사용자 이미지
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;
}
}
어떤 도움 null의 경우
는 첫째로 나는 확인했다?
동적으로 이미지를 만들려고하지 않고 미리 만들어진 26 개의 이미지가 있고 필요한 이미지를 선택하는 것이 더 쉽습니다. – Neil