Reputation: 573
I'm using Firebase Storage to save video/audio user's files. My main idea is to save each video/audio under user ID (UID). It means to each user has several videos/audios files savedd under his UID. I worte a code that the problem is that he keep saving the new video on the old video that i have saved. Please tell me where i'm wrong
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == PICK_VIDEO_REQUEST) {
Uri selectedVideoUri = data.getData();
String userUid = FirebaseAuth.getInstance().getCurrentUser().getUid();
StorageReference storageRef = FirebaseStorage.getInstance().getReference();
videoRef = storageRef.child("/videos/" + userUid );
//TODO: save the video in the db
uploadData(selectedVideoUri);
}else if(requestCode == PICK_AUDIO_REQUEST){
//TODO: save the audio in the db
}
}
}
private void uploadData(Uri videoUri) {
if(videoUri != null){
UploadTask uploadTask = videoRef.putFile(videoUri);
uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if(task.isSuccessful())
Toast.makeText(getContext(), "Upload Complete", Toast.LENGTH_SHORT).show();
progressBarUpload.setVisibility(View.INVISIBLE);
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
updateProgress(taskSnapshot);
}
});
}else {
Toast.makeText(getContext(), "Nothing to upload", Toast.LENGTH_SHORT).show();
}
}
Upvotes: 3
Views: 9683
Reputation: 47
You can use current time to make unique id like this
StorageReference videoRef =storageRef.child(System.currentTimeMillis()+userUid+);
By doing this you will get every time a unique id to save your file . This works for me
Upvotes: 0
Reputation: 317467
You're using the same file path every time for that user's uploads:
videoRef = storageRef.child("/videos/" + userUid);
Instead, you'll need to come up with some unique file name for each upload, and treat the user id as a directory component rather than the name of the file itself:
String filename = "you put a unique file name here";
videoRef = storageRef.child("/videos/" + userUid + "/" + filename);
Upvotes: 7