React Native 应用在 iOS 上上传图片时崩溃,但没有日志记录。

4

当我在Android上尝试使用Firebase uploadBytes功能时,一切正常。但是在iOS上,如果视频文件大小超过2MB,应用程序会崩溃且没有任何日志信息。

我尝试使用uploadBytesResumable,甚至尝试将Firebase版本降级到版本8,但仍未找到解决方案。

const fetchImage = await fetch(localUri);
const imageBlob = await fetchImage.blob();
const storage = getStorage();
const upload = ref(storage, 'path_to_sotrage');
const uploadTask = await uploadBytesResumable(upload, imageBlob);
return await getDownloadURL(uploadTask.ref);

uploadBytesResumable()方法不返回Promise,因此在那里使用await是不必要的。您尝试过使用uploadBytes()吗? - Dharmaraj
你找到解决方案了吗?我这里也遇到同样的问题,即使没有上传,安卓也会崩溃。 - Code Mickey
1个回答

0

这个uploadBytes对我来说总是崩溃,我也不知道为什么。然而,documentation中的uploadBytesResumable模式正在工作,并且似乎更加稳定。希望能有所帮助。

import { getStorage, ref, uploadBytesResumable, getDownloadURL } from "firebase/storage";

const storage = getStorage();

// Create the file metadata
/** @type {any} */
const metadata = {
  contentType: 'image/jpeg'
};

// Upload file and metadata to the object 'images/mountains.jpg'
const storageRef = ref(storage, 'images/' + file.name);
const uploadTask = uploadBytesResumable(storageRef, file, metadata);

// Listen for state changes, errors, and completion of the upload.
uploadTask.on('state_changed',
  (snapshot) => {
    // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
    const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
    console.log('Upload is ' + progress + '% done');
    switch (snapshot.state) {
      case 'paused':
        console.log('Upload is paused');
        break;
      case 'running':
        console.log('Upload is running');
        break;
    }
  }, 
  (error) => {
    // A full list of error codes is available at
    // https://firebase.google.com/docs/storage/web/handle-errors
    switch (error.code) {
      case 'storage/unauthorized':
        // User doesn't have permission to access the object
        break;
      case 'storage/canceled':
        // User canceled the upload
        break;

      // ...

      case 'storage/unknown':
        // Unknown error occurred, inspect error.serverResponse
        break;
    }
  }, 
  () => {
    // Upload completed successfully, now we can get the download URL
    getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => {
      console.log('File available at', downloadURL);
    });
  }
);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接