Java 将文件保存到Firebase存储和数据库-Android

Java 将文件保存到Firebase存储和数据库-Android,java,android,firebase,firebase-realtime-database,firebase-storage,Java,Android,Firebase,Firebase Realtime Database,Firebase Storage,我有一个成功录制视频并在选择后将其保存到Firebase存储的应用程序 但我需要的是将其保存到Firebase数据库。这似乎是我能够在recyclerView中检索/列出视频的唯一方法 我的问题是:如何将保存的文件复制到数据库和存储中 public class FileUploadActivity extends AppCompatActivity { private Button mSelectButton; private Button mPauseButton; private Butt

我有一个成功录制视频并在选择后将其保存到Firebase存储的应用程序

但我需要的是将其保存到Firebase数据库。这似乎是我能够在recyclerView中检索/列出视频的唯一方法

我的问题是:如何将保存的文件复制到数据库和存储中

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 && 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 && 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<UploadTask.TaskSnapshot>() {
                    @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<UploadTask.TaskSnapshot>() {
            @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);

}
}
非常感谢您的建议,因为我不知道如何在recyclerView中查看存储中的文件,只能从数据库中查看


谢谢

这可能是因为您将值设置为null。试着给它一个实际值,它可能会节省。

@LincolnWhite谢谢你。就这么简单。它现在可以工作了我已经增加了你的评论实时数据库不支持空值。这就是为什么什么也救不了。
String uploadId = mDatabaseRef.push().getKey(); 
mDatabaseRef.child(uploadId).setValue(null);