React Native Expo文件系统无法找到或打开本地映像

8zzbczxx  于 6个月前  发布在  React
关注(0)|答案(1)|浏览(82)

我想将存储在root/assets/images/img. png中的图像转换为base64,使用

useEffect(() => {
      const convertImageToBase64 = async () => {
        try {
          const imageUri = 'assets/images/super.jpg'; // Replace with your image path
          const base64 = await FileSystem.readAsStringAsync(FileSystem.documentDirectory+imageUri, {
            encoding: 'base64',
          });
          console.log(base64);
        } catch (error) {
          console.error('Error converting image to base64:', error);
        }
      };
  
      convertImageToBase64(); // Call the conversion function when the component mounts
    }, []);

字符串
[错误:调用函数'ExponentFileSystem.readAsStringAsync'已被拒绝。→引起:java.io.FileNotFoundException:/data/user/0/host. exp.exponent/files/assets/images/super.jpg(No such file or directory)]
我试着这样做,看看它可以加载哪些目录和文件:

useEffect(() => {
      const logFolderPaths = async () => {
        try {
          // Get the document directory
          const documentDirectory = FileSystem.documentDirectory;
          console.log('Document Directory:', documentDirectory);
          await listFilesAndFolders(documentDirectory);
    
          // Get the cache directory
          const cacheDirectory = FileSystem.cacheDirectory;
          console.log('Cache Directory:', cacheDirectory);
          await listFilesAndFolders(cacheDirectory);
    
          // Get the bundled assets directory
          const bundleDirectory = FileSystem.bundleDirectory;
          console.log('Bundle Directory:', bundleDirectory);
          await listFilesAndFolders(bundleDirectory);
    
          // Get the bundled assets directory (expo updates)
          const bundledAssetsDirectory = FileSystem.bundledAssetsDirectory;
          console.log('Bundled Assets Directory:', bundledAssetsDirectory);
          await listFilesAndFolders(bundledAssetsDirectory);
        } catch (error) {
          console.error('Error fetching folder paths:', error);
        }
      };
    
      const listFilesAndFolders = async (directory) => {
        try {
          const result = await FileSystem.readDirectoryAsync(directory);
          console.log(`Files and folders in ${directory}:`, result);
        } catch (error) {
          console.error(`Error listing files and folders in ${directory}:`, error);
        }
      };
    
      logFolderPaths();
    }, []);


文档和缓存目录工作正常,但底部的两个目录返回此错误:Bundle Directory:asset:/ ERROR列出asset:/中的文件和文件夹时出错:[错误:对函数'ExponentFileSystem.readDirectoryAsync'的调用已被拒绝。→引起原因:java.io.IOException:位置'asset:/'的方案不受支持。]
已绑定资产目录:错误:[错误:对函数'ExponentFileSystem.readDirectoryAsync'的调用已被拒绝。→引起:java.lang.NullPointerException:uriString]

pdkcd3nj

pdkcd3nj1#

使用资源库处理静态资源。
资产是与应用的源代码一起存在的任何文件,应用在运行时需要这些文件。
loadAsync将资产数据下载到该高速缓存目录中的本地文件中。

const [{ localUri }] = await Asset.loadAsync(require('./assets/images/super.jpg'));

字符串

相关问题