簡體   English   中英

將文件保存到Firebase存儲和數據庫-Android

[英]Saving files into Firebase storage and database - Android

我有一個可以成功錄制視頻並將其保存到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);

}
}

我試過添加以下內容,但不會執行任何操作:

String uploadId = mDatabaseRef.push().getKey(); 
mDatabaseRef.child(uploadId).setValue(null);

非常感謝收到任何想法,因為我無法解決如何僅從數據庫從recyclerView中的存儲中查看文件。

謝謝

可能是因為您將value設置為null。 嘗試給它一個實際值,它可能會節省。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM