【问题标题】:How to store video in firebase storage using video file path in React-native如何使用 React-native 中的视频文件路径将视频存储在 Firebase 存储中
【发布时间】:2018-05-17 19:10:21
【问题描述】:

当我获得视频路径时,我如何使用react-native 将该视频存储在firebase 存储中?

file://storage/emulated/0/DCIM/1234.MP4....我知道那条路

这是我创建录像机并获取录制的视频文件路径的代码。

import Camera from 'react-native-camera';

    <Camera
                captureMode={this.state.captureMode}
                captureAudio={this.state.captureAudio}
                captureTarget={this.state.captureTarget}
                ref="camera"
                style={styles.preview}>

   { toggle? <TouchableOpacity onPress={() =>this._startRecord()}>
                           <Image style={styles.clickimage}
                             source={require('../images/icon_record_stop.png')} /> 
                      </TouchableOpacity>
            : <TouchableOpacity onPress={() =>this._endVideo()}>
                  <Image style={styles.clickimage}
                             source={require('../images/clickimage.png')} /> 
              </TouchableOpacity>
            }
      </Camera>


     _startRecord() {
      this.refs.camera.capture({mode: Camera.constants.CaptureMode.video})
    .then((data) => {
      this.setState({
          storagepathdata:data.path
        });
    //  var path = data.path;
    })
        .catch((err) => console.log(err))
      }

_endVideo() {
     this.refs.camera.stopCapture()
    .then((response) => {                                      
              alert(this.state.storagepathdata);

         // here storagepathdata is a file path so what i do to store that video on firebase?      
    } 

【问题讨论】:

标签: android firebase react-native firebase-storage


【解决方案1】:

在 fs.readFile(video, 'base64') 中设置视频路径,它将返回 blob,然后设置用于视频的 blob 和 contentType:'video/mp4' 和文件上传后 firebase 返回视频 URL 以响应最后的承诺

别忘了安装和导入

 import RNFetchBlob from 'react-native-fetch-blob';

const sessionId = new Date().getTime();
  const Blob = RNFetchBlob.polyfill.Blob;
  const fs = RNFetchBlob.fs;
  window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest;
  window.Blob = Blob;        
  let uploadBlob = null;
  let mime = 'video/mp4';
  const firebaseRef = firebase.storage().ref("video").child(`${sessionId}`);
  return fs.readFile(video, 'base64') 
  .then((data) => {   
    console.log("data", data)             
      return Blob.build(data, { type: `${mime};BASE64` })
  })
  .then((blob) => {
    console.log("blob", blob)    
      uploadBlob = blob;
      return firebaseRef.put(blob, { contentType: mime })
  })
  .then((res) => {
    console.log("res", res)  
      uploadBlob.close();
      return firebaRef.getDownloadURL()
    })
    .then((res) => {
      console.log("URL", res)

  })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 2017-10-25
    • 2016-10-18
    • 2021-04-26
    相关资源
    最近更新 更多