title: Send Bitmap via e-mail without saving it on device - Android
body: <p>I'm creating an android application where I need to send a picture taken by the camera, but for security reasons I cannot have this picture saved in the users device.</p>
  
  <p>Every solution that I found about sending a Bitmap via e-mail seems to archive it on the app's directory and then use it's path to send it.</p>
  
  <p>In my application the image is only used on this very activity (and when sent via e-mail).</p>
  
  <p>Soo... Is there anyway of sending a Bitmap without saving it?</p>
  
  <p>EDIT:
  Here is how I'm trying to do this as of right now (it's not working btw).</p>
  
  <p>Calling for the camera:</p>
  
  <pre><code>imageButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
          //Opens camera app
          foto          = true;
          Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
          startActivityForResult(intent, 1);
      }
  });
  </code></pre>
  
  <p>Getting the result</p>
  
  <pre><code>protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      //Converts picture to PNG
      if ((requestCode == CAMERA_PIC_REQUEST) &amp;&amp; (resultCode == RESULT_OK)) {
          thumbnail       = (Bitmap)    data.getExtras().get("data");
          ImageView image = (ImageView) findViewById(R.id.image_comment);
          image.setImageBitmap(thumbnail);
  
          try {
              File root = Environment.getExternalStorageDirectory();
              if (root.canWrite()){
                  // We + "/MapTest" to make it storage on a deeper directory for our application
                  problemPicture       = new File(getCacheDir(), "problemPicture.png");
                  FileOutputStream out = new FileOutputStream(problemPicture);
                  thumbnail.compress(Bitmap.CompressFormat.PNG, 100, out);
                  out.flush();
                  out.close();
              }
          } catch (IOException e) {
              Log.e("BROKEN", "Could not write file " + e.getMessage());
          }
      }
  }
  </code></pre>
  
  <p>Sending email (foto is set to true right before it):</p>
  
  <pre><code>public void sendEmail(){
          Intent i = new Intent(Intent.ACTION_SEND);
          if(foto) { //if user attached a pic
              i.setType("image/png");
              i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"sample_mail@gmail.com"});
              i.putExtra(Intent.EXTRA_SUBJECT, "Assistencia Tecnica");
              i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(problemPicture));
              i.putExtra(Intent.EXTRA_TEXT, commentText.getText());
              problemPicture.delete();
              try {
                  startActivity(Intent.createChooser(i, "Enviando Email..."));
              } catch (android.content.ActivityNotFoundException ex) {
                  Toast.makeText(StatusActivity.this, "Comunicaçaõ Falhou",
                          Toast.LENGTH_SHORT).show();
              }
              try {
                  // We try to delete the picture
                  boolean success = problemPicture.delete();
                  if(success) {
                      Log.i(TAG, "File deleted successfully");
                  } else {
                      if(problemPicture.isDirectory()) {
                          success = deleteDirectory(problemPicture);
                          if(success) {
                              Log.i(TAG, "Directory deleted successfully!");
                          } else {
                              Log.i(TAG, "Could not delete directory");
                          }
                      }
                      Log.i(TAG, "File could not be deleted");
                  }
              } catch (NullPointerException e) {
                  // In case we don't find it
                  Log.i(TAG, "File not found");
              }
  
              try {
                  deleteFile("problemPicture.png");
              } catch(NullPointerException e) {
                  Log.i(TAG, "Context file not found");
              }
          }
          else {
              i.setType("plane/text");
              i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"sample_email@gmail.com"});
              i.putExtra(Intent.EXTRA_SUBJECT, "Assistencia Tecnica");
              i.putExtra(Intent.EXTRA_TEXT, commentText.getText());
              try {
                  startActivity(Intent.createChooser(i, "Enviando Email..."));
              } catch (android.content.ActivityNotFoundException ex) {
                  Toast.makeText(StatusActivity.this, "Comunicaçaõ Falhou", Toast.LENGTH_SHORT).show();
              }
          }
      }
  </code></pre>
  
  <p>Ending activity</p>
  
  <pre><code>private void endActivity() {
      //Back to Maps Activity
      boolean success = false;
      try {
          success = problemPicture.delete();
      } catch(NullPointerException e) {
          Log.d(TAG, "No picture found");
      }
      if(success) {
          Log.i(TAG, "Picture deleted");
      } else {
          Log.i(TAG, "Picture could not be deleted");
      }
  
      success = deleteFile("problemPicture.png");
      if(success) {
          Log.d(TAG, "File deleted");
      } else {
          Log.d(TAG, "File could not be deleted");
      }
  
      Toast.makeText(getApplicationContext(), "OS Finalizada",Toast.LENGTH_SHORT).show();
      Intent it = new Intent(StatusActivity.this, MapsActivity.class)
              .putExtra("index", index);
      startActivity(it);
  }
  </code></pre>
  
  <p>Sorry for the wall of bad code, and thank you!</p>