title: Saving files into Firebase storage and database - Android
body: <p>I have an app that successfully records video and saves it to Firebase storage upon selection.</p>
  
  <p>What I need though is for this to be saved to the Firebase database. It seems like this is the only way that I would be able to retrieve/list the videos in a recyclerView.</p>
  
  <p>My question is: How do I duplicate the saved file into the database as well as in the storage?</p>
  
  <pre><code>public class FileUploadActivity extends AppCompatActivity {
  
  private Button mSelectButton;
  private Button mPauseButton;
  private Button mCancelButton;
  
  private final static int FILE_SELECT_CODE = 1;
  
  private StorageReference mStorageRef;
  private ProgressBar mProgress;
  
  private TextView mSizeLabel;
  private TextView mProgressLabel;
  
  private TextView mFilenameLabel;
  private TextView mUploadHeading;
  
  private StorageTask mStorageTask;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_file_upload);
  
      mSelectButton = (Button) findViewById(R.id.videoUploadBtn);
      mPauseButton = (Button) findViewById(R.id.pauseUploadBtn);
      mCancelButton = (Button) findViewById(R.id.cancelUploadBtn);
  
      mFilenameLabel = (TextView) findViewById(R.id.tvFilenameLabel);
      mUploadHeading = (TextView) findViewById(R.id.tvUploadHeading);
  
      mProgress = (ProgressBar) findViewById(R.id. uploadProgressBar);
  
      mSizeLabel = (TextView) findViewById(R.id.tvSizeLabel);
      mProgressLabel = (TextView) findViewById(R.id.tvProgressLabel);
  
      mStorageRef = FirebaseStorage.getInstance().getReference();
  
      mSelectButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
  
              openFileSelector();
  
          }
      });
  
      mPauseButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
  
              String btnText = mPauseButton.getText().toString();
  
              if (btnText.equals("Pause Upload")) {
  
                  mStorageTask.pause();
                  mPauseButton.setText("Resume Upload");
  
              } else {
  
                  mStorageTask.resume();
                  mPauseButton.setText("Pause Upload");
  
              }
          }
      });
  
      mCancelButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              mStorageTask.cancel();
              Toast.makeText(FileUploadActivity.this, "Video Upload Cancelled", Toast.LENGTH_SHORT).show();
          }
      });
  
  
  }
  
  private void openFileSelector() {
  
      Intent intent  = new Intent(Intent.ACTION_GET_CONTENT);
      intent.setType("*/*");
      intent.addCategory(Intent.CATEGORY_OPENABLE);
  
      try {
          startActivityForResult(
                  Intent.createChooser(intent, "Select a File to Upload"),
                  FILE_SELECT_CODE);
      } catch (android.content.ActivityNotFoundException ex) {
          Toast.makeText(this, "Please install a File Manager", Toast.LENGTH_SHORT).show();
      }
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  
      if (requestCode == FILE_SELECT_CODE &amp;&amp; resultCode == RESULT_OK) {
  
          Uri fileUri = data.getData();
  
          String uriString = fileUri.toString();
  
          File myFile = new File(uriString);
          //String path = myFile.getAbsolutePath();
  
          String displayName = null;
  
          String headingName = "Your File is Uploading";
  
          if (uriString.startsWith("content://")) {
              Cursor cursor = null;
              try {
                  cursor = FileUploadActivity.this.getContentResolver().query(fileUri, null,
                          null, null, null);
                  if (cursor != null &amp;&amp; cursor.moveToFirst()) {
                      displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                  }
              } finally {
                  cursor.close();
              }
          } else if (uriString.startsWith("file://")) {
              displayName = myFile.getName();
          }
  
          mFilenameLabel.setText(displayName);
          mUploadHeading.setText(headingName);
  
          StorageReference riversRef = mStorageRef.child("files/" + displayName);
  
          mStorageTask = riversRef.putFile(fileUri)
                  .addOnSuccessListener(new OnSuccessListener&lt;UploadTask.TaskSnapshot&gt;() {
                      @Override
                      public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                          // Get a URL to the uploaded content
                          Uri downloadUrl = taskSnapshot.getDownloadUrl();
  
                          Toast.makeText(FileUploadActivity.this, "File Uploaded",
                                  Toast.LENGTH_SHORT).show();
                      }
                  })
                  .addOnFailureListener(new OnFailureListener() {
                      @Override
                      public void onFailure(@NonNull Exception exception) {
                          // Handle unsuccessful uploads
                          //Toast.makeText(FileUploadActivity.this, "There was an error in uploading file",
                            //      Toast.LENGTH_SHORT).show();
                          // ...
                      }
                  }).addOnProgressListener(new OnProgressListener&lt;UploadTask.TaskSnapshot&gt;() {
              @Override
              public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
  
                  double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
  
                  mProgress.setProgress((int) progress);
  
                  String progressText = taskSnapshot.getBytesTransferred()/(1024 * 1024) + " / " +
                          taskSnapshot.getTotalByteCount()/(1024 * 1024) + " mb";
  
                  mSizeLabel.setText(progressText);
  
              }
          });
  
      }
  
      super.onActivityResult(requestCode, resultCode, data);
  
  }
  }
  </code></pre>
  
  <p>I've tried adding the following but that doesn't do anything:</p>
  
  <pre><code>String uploadId = mDatabaseRef.push().getKey(); 
  mDatabaseRef.child(uploadId).setValue(null);
  </code></pre>
  
  <p>Any ideas gratefully received as I can't work out how to view files from storage in a recyclerView, only from the database.</p>
  
  <p>Thanks</p>