问题
I am able to uploading/downloading images from firebase but do not know how upload video or .mp3 file on firebase in android.so suggest me. Thanks in advance.
回答1:
Rewriting the example from the docs based on the answer given in Uploading MP3 file to Firebase Storage ends up being a video/mp3 file, it seems that a full working example of an audio upload would look something like this:
// File or Blob
file = Uri.fromFile(new File("path/to/audio.mp3"));
// Create the file metadata
metadata = new StorageMetadata.Builder()
.setContentType("audio/mpeg")
.build();
// Upload file and metadata to the path 'audio/audio.mp3'
uploadTask = storageRef.child("audio/"+file.getLastPathSegment()).putFile(file, metadata);
// Listen for state changes, errors, and completion of the upload.
uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
System.out.println("Upload is " + progress + "% done");
}
}).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
@Override
public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
System.out.println("Upload is paused");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Handle successful uploads on complete
Uri downloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
}
});
来源:https://stackoverflow.com/questions/40146354/how-upload-video-mp3-file-on-firebase-android