无法在 React Native 中使用 rn-fetch-blob 保存文件

Can't Save File With rn-fetch-blob in React Native

提问人:Jorpy 提问时间:10/23/2023 更新时间:10/23/2023 访问量:40

问:

无法使用 rn-fetch-blob 保存文件。我正在使用 react-native-image-crop-picker 选择图像并使用方法保存裁剪后的图像。它记录“图像保存在:/data/user/0/com.cropimage/files/1698007097111.jpg”。但是在设备上我看不到保存的图像。我被添加WRITE_EXTERNAL_STORAGE权限。cp()

import { Button, View } from 'react-native';
import React from 'react';
import ImagePicker from 'react-native-image-crop-picker';
import RNFetchBlob from 'rn-fetch-blob';

export default function App() {
  const saveLocalImage = (localImagePath: string, destinationPath: string) => {
    RNFetchBlob.fs
      .cp(localImagePath, destinationPath)
      .then(() => {
        console.log('Image saved at:', destinationPath);
      })
      .catch((error) => {
        console.error('Error saving image:', error);
      });
  };

  function pickImage() {
    ImagePicker.openPicker({
      cropping: true,
      freeStyleCropEnabled: true,
    }).then(image => {
      const sourceUri = image.path;
      const timestamp = new Date().getTime();
      const destinationPath = `${RNFetchBlob.fs.dirs.DocumentDir}/${timestamp}.jpg`;
      saveLocalImage(sourceUri, destinationPath);
    });
  }

  return (
    <View>
      <Button
        onPress={pickImage}
        title="Pick image"
        color="#841584"
        accessibilityLabel="Learn more about this purple button"
      />
    </View>
  );
}

reactjs 反应原生

评论


答:

1赞 Shivo'ham 10/23/2023 #1

使用 saveToCameraRoll 方法而不是 RNFetchBlob

 CameraRoll.save(URL)
    .then(alert('Done', 'image download successfully')) 
    .catch(err => console.log(err))

文档链接