public void loadImage() {
    String nameFile = User.getInstance().getFriendsHashMap().get(String.valueOf(toID)).getImage();

    if (!nameFile.equals("") && !nameFile.equals(null)) {
      File ruta_sd = Environment.getExternalStorageDirectory();

      String ruta = ruta_sd.getAbsolutePath() + "/IMAGES_CHAT_ANDROID/" + nameFile + ".jpg";

      File file = new File(ruta);

      if (file.exists()) {

        Bitmap photobmp = BitmapFactory.decodeFile(ruta);
        userImage.setImageBitmap(photobmp);

        intentViewImages = new Intent(this, ViewImage.class);
        intentViewImages.putExtra("path", ruta);

        userImage.setOnClickListener(
            new View.OnClickListener() {
              public void onClick(View v) {

                startActivity(intentViewImages);
              }
            });
      }
    }
  }
  private void showMessage(Message message) {
    LinearLayout.LayoutParams params =
        new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    params.setMargins(0, 0, 0, 20);

    TextView textView = new TextView(ChatActivity.this);

    String nameSender;

    if (message.getReceiver() != user.getID()) {
      params.gravity = Gravity.RIGHT;
      textView.setBackgroundResource(R.drawable.message_1);

      nameSender = user.getNick();
    } else {
      params.gravity = Gravity.LEFT;
      textView.setBackgroundResource(R.drawable.message_2);

      nameSender =
          User.getInstance()
              .getFriendsHashMap()
              .get(String.valueOf(message.getIdFriend()))
              .getNick();
    }

    String text =
        "<font color=#161F89><small><b>"
            + nameSender
            + ":"
            + "</b></small></font><br/>"
            + message.getText();

    textView.setText(Html.fromHtml(text));

    textView.setLayoutParams(params);
    messagesLayout.addView(textView);

    textView.setPadding(16, 16, 16, 16);
    textView.setTextSize(16);

    keyboard.setText("");
    try {
      scrollView.post(
          new Runnable() {
            @Override
            public void run() {
              scrollView.fullScroll(View.FOCUS_DOWN);
            }
          });
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public void showNotification(Message message) {
    Intent intent = new Intent(this, Notification.class);

    boolean isGroup = message.getIsGroup();
    int notificationID = isGroup ? message.getReceiver() : message.getIdFriend();

    intent.putExtra("notificationID", notificationID);
    intent.putExtra("isGroup", isGroup);

    PendingIntent pendingIntent =
        PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    String name = "";

    if (isGroup) {
      Group group = user.getGroupsHashMap().get(String.valueOf(notificationID));
      name = group.getName();
    } else {
      name = User.getInstance().getFriendsHashMap().get(String.valueOf(notificationID)).getNick();
    }

    CharSequence ticker = message.getText();
    CharSequence contentTitle = "Android Chat";
    CharSequence contentText = "Message from " + name;

    NotificationCompat.Builder noti =
        new NotificationCompat.Builder(this)
            .setContentIntent(pendingIntent)
            .setTicker(ticker)
            .setContentTitle(contentTitle)
            .setContentText(contentText)
            // .setLights(Color.RED, 1, 0)
            .setSmallIcon(R.mipmap.ic_ini)
            .setPriority(android.app.Notification.PRIORITY_MAX)
            .setAutoCancel(true)
            .addAction(R.mipmap.ic_ini, ticker, pendingIntent)
            .setVibrate(new long[] {100, 250, 100, 500});

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(notificationID, noti.build());
    ringtone.play();
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mGoogleApiClient =
        new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

    locationRequest =
        LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(20000)
            .setFastestInterval(10000);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    toID = getIntent().getExtras().getInt("toID");
    user = User.getInstance();
    friend = user.getFriendsHashMap().get(String.valueOf(toID));

    popupMenu = new PopupMenu(this, toolbar, Gravity.RIGHT);
    popupMenu.setOnMenuItemClickListener(this);
    popupMenu.inflate(R.menu.popup);

    ImageButton sendButton = (ImageButton) findViewById(R.id.chat_send);
    keyboard = (EditText) findViewById(R.id.chat_keyboard);
    scrollView = (ScrollView) findViewById(R.id.chat_scroll);
    messagesLayout = (LinearLayout) findViewById(R.id.chat_messages);
    userImage = (ImageView) findViewById(R.id.chat_user_image);
    userName = (TextView) findViewById(R.id.chat_user_name);
    ImageButton attachButton = (ImageButton) findViewById(R.id.chat_attach);

    sendButton.setOnClickListener(this);
    attachButton.setOnClickListener(this);

    changeToolBar();

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    String alarms = preferences.getString("message-notification-sound", "default ringtone");

    Uri uri = Uri.parse(alarms);
    ringtone = RingtoneManager.getRingtone(this, uri);

    if (!isStarted) {
      messages = new MessageList();
      LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
      isStarted = true;
      service = new Service();

      try {
        queue.put(service);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      PausableThreadPool executor = new PausableThreadPool(2, 2, 10, TimeUnit.SECONDS, queue);
      executor.execute(service);

      threadReceiver = new Thread(Receiver);
      threadReceiver.start();
    } else {
      while (threadReceiver.isAlive()) {
        threadReceiver.interrupt();
      }

      threadReceiver = new Thread(Receiver);
      threadReceiver.start();
    }

    loadDBMessages();

    try {
      scrollView.post(
          new Runnable() {
            @Override
            public void run() {
              scrollView.fullScroll(View.FOCUS_DOWN);
            }
          });
    } catch (Exception e) {
      e.printStackTrace();
    }

    loadImage();
  }