title: An android application that chooses photo from gallery and uploads into the firebase batabase
body: <p>Like I said in my title I am trying to learn to use databases in android application. So I thought of creating a sample application that takes photos from gallery and uploads the image into the firebase database. However, my application collapses itself after I select a photo from gallery.</p>
  
  <p>Here is my android manifest file:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.trump.demo_cameraapp"&gt;
  
      &lt;uses-permission android:name="android.permission.CAMERA"/&gt;
  
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:roundIcon="@mipmap/ic_launcher_round"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"&gt;
          &lt;activity android:name=".MainActivity"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;provider
              android:name="android.support.v4.content.FileProvider"
              android:authorities="${applicationId}"
              android:exported="false"
              android:grantUriPermissions="true"&gt;
  
              &lt;meta-data
                  android:name="android.support.FILE_PROVIDER_PATHS"
                  android:resource="@xml/file_paths" /&gt;
          &lt;/provider&gt;
      &lt;/application&gt;
  
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Here is my layout file, activity_main.xml:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_height="match_parent"
      android:layout_width = "match_parent"
      android:gravity="center"
      android:orientation="vertical"&gt;
  
      &lt;ImageView
          android:id="@+id/imageView1"
          android:layout_height="match_parent"
          android:layout_width="match_parent"
          android:layout_weight="2"&gt;
  
      &lt;/ImageView&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Choose Photo!"
          android:textColor="@color/Blue"
          android:id="@+id/chooseButton"/&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text = "Upload Photo!"
          android:textColor="@color/colorPrimaryDark"
          android:id="@+id/uploadButton"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>I have the following lines of code in MainActivity</p>
  
  <pre><code>package com.example.trump.demo_cameraapp;
  
  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.content.Intent;
  import android.graphics.Bitmap;
  import android.net.Uri;
  import android.os.Environment;
  import android.provider.MediaStore;
  import android.support.v4.content.FileProvider;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.ImageView;
  import android.widget.Toast;
  
  import com.google.android.gms.tasks.OnSuccessListener;
  import com.google.firebase.storage.FirebaseStorage;
  import com.google.firebase.storage.StorageReference;
  import com.google.firebase.storage.UploadTask;
  
  import java.io.ByteArrayOutputStream;
  import java.io.File;
  import java.io.IOException;
  
  
  public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  
      private static final int PICK_IMAGE_REQUEST = 1;
      int TAKE_PHOTO_CODE = 0;
      public static int count = 0;
      private ImageView mImageView;
      private StorageReference storage;
      private ProgressDialog mProgressDialog;
      public static final int GALLERY_INTENT = 2;
  
      /**
       * Called when the activity is first created.
       */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          // Here, we are making a folder named picFolder to store
          // pics taken by the camera using this application.
  
  
         mImageView = (ImageView) findViewById(R.id.imageView1);
  
  
         Button choose = (Button) findViewById(R.id.chooseButton);
          choose.setOnClickListener(this);
  
          Button upload = (Button) findViewById(R.id.uploadButton);
          upload.setOnClickListener(this);
  
      }
  
      private void showFileChooser(){
          Intent intent = new Intent();
          intent.setType("image/*");
          intent.setAction(Intent.ACTION_GET_CONTENT);
          startActivityForResult(Intent.createChooser(intent,"Select an image"),PICK_IMAGE_REQUEST);
      }
  
  
  
      @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) {
              Log.d("ResultOK", String.valueOf(RESULT_OK));
              Log.d("CameraDemo", "Pic saved");
              mProgressDialog.setMessage("Uploading...");
              mProgressDialog.show();
              Uri uri = data.getData();
  
              try {
                  Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                  mImageView.setImageBitmap(bitmap);
              } catch (IOException e) {
                  e.printStackTrace();
              }
  
              StorageReference filepath = storage.child("images").child(uri.getLastPathSegment());
              filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener&lt;UploadTask.TaskSnapshot&gt;(){
  
                  @Override
                  public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                      Toast.makeText(MainActivity.this, "Upload Successful", Toast.LENGTH_SHORT).show();
                      mProgressDialog.dismiss();
                  }
              });
  
          }
      }
  
      @Override
      public void onClick(View view) {
          if(view.getId() == R.id.chooseButton){
              showFileChooser();
  
  
          }else if(view.getId() == R.id.uploadButton){
  
  
          }
  
      }
  }
  </code></pre>
  
  <p>I have not yet written codes to upload the image on firebase database. All I am trying to do right now is to display the image I picked from gallery in the imageview of my app. However, like I said before it's not displaying anything right now and as soon as I choose some photo from gallery it collapses. Any help regarding the issue would be very appreciated.</p>