gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:01:14 24 4
gpt4 key购买 nike

我有一个应用可以成功录制视频并根据选择将其保存到 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 中存储的文件。

谢谢

最佳答案

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

关于java - 将文件保存到 Firebase 存储和数据库中 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49019712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com