How upload video/mp3 file on firebase android

假如想象 提交于 2020-01-04 05:46:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!