androidstringbitmapimageviewdynamic-image-generation

How to Convert String "A" into Image in android?


I am making a chat application. I have one imageview for users profile.

If users image is null/empty, I want show first letter of users name, for example: suppose users name is "UNKNOWN" so i want to show only U on that image view.
Click here to see sample of what I want

I have tried :

Firstly I have checked if users image is null

  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;
        }
    }

Any help?


Solution

  • Use textview to show the first letter and add a background color to it. Use Random class to get a random color as background each time.

      private String senderFirstLetter;
            private TextView senderProfilePic;
    // To retrieve first letter of the sender,
        senderFirstLetter = (String) holder.sender.getText().subSequence(0, 1);
                holder.senderProfilePic.setText(senderFirstLetter);
    // To get random color 
        GradientDrawable drawable = (GradientDrawable) holder.senderProfilePic.getBackground();
                Random randomBackgroundColor = new Random();
                int color = Color.argb(255, randomBackgroundColor.nextInt(256), randomBackgroundColor.nextInt(256), randomBackgroundColor.nextInt(256));
                drawable.setColor(color);