title: Android Studio - Long delay when selecting image through gallery
body: <p>New to Android Studio, just wondering if anyone can help on this delay when I send an image that is taken from the gallery. Once image is selected there is a long delay until it is actually sent, and for a moment the screen goes black as well. </p>
  
  <p>Here is the code - thanks </p>
  
  <pre><code>public boolean onOptionsItemSelected(MenuItem item) {
      if (item.getItemId() == R.id.launch_voip_call) {
          Utils.startCall(this, contact);
          return true;
      } else if (item.getItemId() == R.id.launch_camera) {
          AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setMessage("Pick Image from")
                  .setCancelable(false)
                  .setPositiveButton("Camera", new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int id) {
                          //camera intent
                          Intent cameraIntent = new Intent(ConversationActivity.this, CameraActivity.class);
                          cameraIntent.putExtra("EXTRA_CONTACT_JID", contact.getJid());
                          startActivity(cameraIntent);
                      }
                  })
                  .setNegativeButton("Gallery", new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int id) {
                          Intent intent = new Intent();
                          // Show only images, no videos or anything else
                          intent.setType("image/*");
                          intent.setAction(Intent.ACTION_GET_CONTENT);
                          // Always show the chooser (if there are multiple options available)
                          startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
  
                      }
                  });
          AlertDialog alert = builder.create();
          alert.show();
  
  
      }
      return false;
  }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
  
      if (requestCode == PICK_IMAGE_REQUEST &amp;&amp; resultCode == RESULT_OK &amp;&amp; data != null &amp;&amp; data.getData() != null) {
  
          Uri uri = data.getData();
  
          try {
              Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
              ByteArrayOutputStream stream = new ByteArrayOutputStream();
              bitmap.compress(Bitmap.CompressFormat.WEBP, 5, stream);
              byte[] byteArray = stream.toByteArray();
              // Log.d(TAG, String.valueOf(bitmap));
              EventBus.getDefault().post(new MessageEvent.SendMessage(contact.getJid(), byteArray, ""));
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  
  @OnTextChanged(R.id.compose)
  public void onMessageChanged(CharSequence s) {
      sendButton.setVisibility(s.length() &gt; 0 ? View.VISIBLE : View.INVISIBLE);
  }
  
  @OnClick(R.id.send)
  public void onSend(final View view) {
      String message = composeText.getText().toString().trim();
      if (!message.isEmpty()) {
          if (!amIOffline()) {
              EventBus.getDefault().post(new MessageEvent.SendMessage(contact.getJid(), message));
              composeText.setText("");
          } else {
              SimpleSnackbar.offlineGroupChat(view).show();
          }
      }
  }
  </code></pre>