You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取React Native项目目录路径并用于显示图片?

Android项目assets目录结构

我的图片存放在Android项目的assets文件夹中,想要不依赖require函数来使用这些图片。

已知可以通过file://{ProjectDir}+{pathToImage}格式的ImageUri访问图片,这种方式无需require就能直接引用,但我没法确定正确的图片路径,已经做了多次尝试:

尝试的示例代码

  1. 示例1:
const documentDirectoryPath = RNFS.MainBundlePath;
const relativePath = 'assets/your_file.jpg';
const absolutePath = `${documentDirectoryPath}/${relativePath}`;

console.log('Absolute path:', absolutePath);
  1. 示例2:
import RNFS from 'react-native-fs';

const baseFilePath = RNFS.DocumentDirectoryPath;
const subdirectoryPath = `${baseFilePath}/your_subdirectory_name`;
const filePath = `${subdirectoryPath}/your_file_name.ext`;

console.log('Absolute file path:', filePath);

内容的提问来源于stack exchange,提问作者Dhaval Panchani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 02:04:57