title: Capturing Image from Camera and Upload to Firebase
body: <p>I am working on a simple blog app that allows the user to upload photo from phone gallery and descriptions to the Firebase Server. I am trying to modify my current project to allow the user to capture photo from camera and uploading it to the firebase server. </p>
  
  <p>Currently, I am able to display the image that i have captured into imagebutton, however i am unable to post my image to the firebase server (The "submit post" button does not react to my onclick function). </p>
  
  <p>I am suspecting there is some error in my startPosting() function or i did not encode the image correctly? Please help. </p>
  
  <pre><code>package simpleblog2.emily.example.com.simpleblog2;
  
  import android.app.ProgressDialog;
  import android.content.ContentProvider;
  import android.content.Intent;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.media.Image;
  import android.net.Uri;
  import android.os.Environment;
  import android.provider.MediaStore;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.text.TextUtils;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ImageButton;
  import android.widget.Toast;
  
  import com.google.android.gms.tasks.OnSuccessListener;
  import com.google.firebase.database.DatabaseReference;
  import com.google.firebase.database.FirebaseDatabase;
  import com.google.firebase.storage.FirebaseStorage;
  import com.google.firebase.storage.StorageReference;
  import com.google.firebase.storage.UploadTask;
  import com.squareup.picasso.Picasso;
  
  import com.theartofdev.edmodo.cropper.CropImage;
  
  import java.io.File;
  
  
  public class PostActivity extends AppCompatActivity {
  
      private ImageButton mSelectImage;
      private EditText mPostTitle;
      private EditText mPostDesc;
      private Button mSubmitBtn;
      private ProgressDialog mProgress;
      private DatabaseReference mDatabase;
  
      private Uri mImageUri = null;
  
      private static final int GALLERY_REQUEST = 1;
  
      private static final int CAMERA_REQUEST_CODE = 1;
  
      private StorageReference mStorage;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_post);
  
          mStorage = FirebaseStorage.getInstance().getReference();
          mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
  
          mSelectImage = (ImageButton) findViewById(R.id.imageSelect);
          mPostTitle = (EditText) findViewById(R.id.titleField);
          mPostDesc = (EditText) findViewById(R.id.descField);
          mSubmitBtn = (Button) findViewById(R.id.submitBtn);
  
          mProgress = new ProgressDialog(this);
  
          mSelectImage.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                  startActivityForResult(intent1, CAMERA_REQUEST_CODE);
                  intent1.setType("image/*");
  
                  /*
                  Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
                  galleryIntent.setType("image/*");
                  startActivityForResult(galleryIntent, GALLERY_REQUEST);
                  */
              }
          });
  
          mSubmitBtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  startPosting();
              }
          });
      }
  
      private void startPosting() {
  
          mProgress.setMessage("Posting to blog...");
  
  
          final String title_val = mPostTitle.getText().toString().trim();
          final String desc_val = mPostDesc.getText().toString().trim();
          if (!TextUtils.isEmpty(title_val) &amp;&amp; !TextUtils.isEmpty(desc_val) &amp;&amp; mImageUri != null) {
  
              mProgress.show();
              StorageReference filepath = mStorage.child("Blog_Images").child(mImageUri.getLastPathSegment());
  
              filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener&lt;UploadTask.TaskSnapshot&gt;() {
                  @Override
                  public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                      Uri downloadUrl = taskSnapshot.getDownloadUrl();
  
                      DatabaseReference newPost = mDatabase.push();
                      newPost.child("title").setValue(title_val);
                      newPost.child("desc").setValue(desc_val);
                      newPost.child("image").setValue(downloadUrl.toString());
  
  
                      mProgress.dismiss();
                      startActivity(new Intent(PostActivity.this, MainActivity.class));
                  }
              });
          }
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
  
  
  
  
          // if(requestCode == GALLERY_REQUEST &amp;&amp; resultCode == RESULT_OK){
          if (requestCode == CAMERA_REQUEST_CODE &amp;&amp; resultCode == RESULT_OK) {
  
        /*   mImageUri = data.getData();
              mSelectImage.setImageURI(mImageUri);
  
              CropImage.activity(mImageUri)
                      .setGuidelines(CropImageView.Guidelines.ON)
  
                      .start(this);
  
  */
  
              Bitmap mImageUri = (Bitmap) data.getExtras().get("data");
              mSelectImage.setImageBitmap(mImageUri);
         }
  
  
          if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
              CropImage.ActivityResult result = CropImage.getActivityResult(data);
              if (resultCode == RESULT_OK) {
                  Uri resultUri = result.getUri();
                  mSelectImage.setImageURI(resultUri);
              } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                  Exception error = result.getError();
              }
          }
  
  
      }
  
  
  
  }
  </code></pre>