CodeLinkIO / Firebase-Image-Upload-React-Native

Demo to handle image with Firebase storage
129 stars 22 forks source link

hi guys, how can i upload the image after reducing size. #12

Open priya-vr opened 6 years ago

priya-vr commented 6 years ago

I am using image picker and RNFetchBlob. before uploading image i reduce the size of the image . const options = { cameraType: 'back', mediaType: 'mixed', maxWidth:50, maxHeight:50, width:50, height:50, allowsEditing:true, noData:true, storageOptions: { cameraRoll: false, // Don't save photos to the camera roll path: "/data/data/com.testapp/itemfiles", skipBackup: true, // No need to backup to iClould }, }; uploading image

const Blob = RNFetchBlob.polyfill.Blob; const fs = RNFetchBlob.fs; window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest; window.Blob = Blob; let uploadBlob = null const imageRef = firebase.storage().ref().child('images/') let mime = 'image/jpg' fs.readFile(image.uri, 'base64') .then((data) => { return Blob.build(data, { type: ${mime};BASE64 }) }) .then((blob) => { uploadBlob = blob return imageRef.put(blob, { contentType: mime }) }) .then(() => { uploadBlob.close() return imageRef.getDownloadURL() }) .then((url) => { // URL of the image uploaded on Firebase storage console.log(url); this.getRef().child('personalChat/' + this.generateChatId()).push({ _id: now , text: message.text != null ? message.text : '', createdAt: now, uid: this.user.uid, senderName: this.state.senderName, receiverName:this.state.receiverName, receiverUid:this.friend.uid, order: -1 * now, image:url,

           })

  })
  .catch((error) => {
    console.log(error);

  })

Image picker response {height: 33, origURL: "assets-library://asset/asset.JPG?id=B84E8479-475C-4727-A4A4-B77AA9980897&ext=JPG", uri: "file:///Users/mac/Library/Developer/CoreSimulator/…temfiles/3F33BD6C-0232-415E-8642-1FD043412478.jpg", fileName: "IMG_0002.JPG", width: 50, …}

But i am not able to upload the image using image.uri in RNFetchBlob. How can i upload the image using my specified size .Please help me.