我有一个成功录制视频的应用,并在选择后将其保存到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中查看存储中的文件。
由于
答案 0 :(得分:2)
可能是因为您将值设置为null。尝试给它一个实际值,它可能会保存。