This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 55).
Expo 资源
一个允许下载资源并与其他库一起使用的通用库。
For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.
expo-asset 提供了一个与 Expo 资源系统交互的接口。资源是指任何与应用源代码放在一起、且应用在运行时需要的文件。例如图片、字体和声音。Expo 的资源系统与 React Native 的资源系统集成,因此你可以使用 require('path/to/file') 来引用文件。例如,这就是你在 React Native 中为 Image 组件引用静态图片文件的方式。有关更多信息,请查看 React Native 关于静态图片资源的文档。这种引用静态图片资源的方法在 Expo 中开箱即用。
安装
- npx expo install expo-assetIf you are installing this in an existing React Native app, make sure to install expo in your project.
在 app config 中进行配置
如果你的项目使用 config plugins(Continuous Native Generation (CNG)),你可以使用 expo-asset 内置的 config plugin 来进行配置。该插件允许你配置一些无法在运行时设置、且需要重新构建新的应用二进制文件后才会生效的属性。如果你的应用不使用 CNG,那么你需要手动配置该库。
Example app.json with config plugin
{ "expo": { "plugins": [ [ "expo-asset", { "assets": ["path/to/file.png", "path/to/directory"] } ] ] } }
Configurable properties
| Name | Default | Description |
|---|---|---|
assets | [] | 一个资源文件或目录的数组,用于链接到原生项目。路径应相对于项目根目录,这样无论是直接指定文件还是使用目录,文件名都会成为资源名称。 支持的文件类型:
|
用法
了解更多关于如何使用 expo-asset config plugin 将资源文件嵌入到项目中的内容,请参阅 在构建时加载资源。
API
import { Asset } from 'expo-asset';
Hooks
| Parameter | Type |
|---|---|
| moduleIds | number | number[] |
Downloads and stores one or more assets locally. After the assets are loaded, this hook returns a list of asset instances. If something went wrong when loading the assets, an error is returned.
Note, the assets are not "reloaded" when you dynamically change the asset list.
Returns an array containing:
- on the first position, a list of all loaded assets. If they aren't loaded yet, this value is
undefined. - on the second position, an error which encountered when loading the assets. If there was no
error, this value is
undefined.
Example
const [assets, error] = useAssets([require('path/to/asset.jpg'), require('path/to/other.png')]); return assets ? <Image source={assets[0]} /> : null;
Classes
The Asset class represents an asset in your app. It gives metadata about the asset (such as its
name and type) and provides facilities to load the asset data.
Asset Properties
boolean • Default: falseWhether the asset has finished downloading from a call to downloadAsync().
union • Default: nullThe MD5 hash of the asset's data.
Acceptable values are: string | null
union • Default: nullIf the asset is an image, the height of the image data divided by the scale factor. The scale factor is the number after @ in the filename, or 1 if not present.
Acceptable values are: number | null
union • Default: nullIf the asset has been downloaded (by calling downloadAsync()), the
file:// URI pointing to the local file on the device that contains the asset data.
Acceptable values are: string | null
stringThe name of the asset file without the extension. Also without the part from @ onward in the
filename (used to specify scale factor for images).
stringA URI that points to the asset's data on the remote server. When running the published version of your app, this refers to the location on Expo's asset server where Expo has stored your asset. When running the app from Expo CLI during development, this URI points to Expo CLI's server running on your computer and the asset is served directly from your computer. If you are not using Classic Updates (legacy), this field should be ignored as we ensure your assets are on device before running your application logic.
Asset Methods
Downloads the asset data to a local file in the device's cache directory. Once the returned
promise is fulfilled without error, the localUri field of this asset points
to a local file containing the asset data. The asset is only downloaded if an up-to-date local
file for the asset isn't already present due to an earlier download. The downloaded Asset
will be returned when the promise is resolved.
Note: There is no guarantee that files downloaded via
downloadAsyncpersist between app sessions.downloadAsyncstores files in the caches directory, so it's up to the OS to clear this folder at its own discretion or when the user manually purges the caches directory. Downloaded assets are stored asExponentAsset-{cacheFileId}.{extension}within the cache directory. To manually clear cached assets, you can useexpo-file-systemto delete the cache directory:Paths.cache.delete()or use the legacy APIdeleteAsync(cacheDirectory).
Returns a Promise which fulfills with an Asset instance.
| Parameter | Type | Description |
|---|---|---|
| virtualAssetModule | string | number | {
height: number,
uri: string,
width: number
} | The value of |
| Parameter | Type | Description |
|---|---|---|
| moduleId | string | number | number[] | string[] | An array of |
Types
| Property | Type | Description |
|---|---|---|
| hash(optional) | string | null | - |
| height(optional) | number | null | - |
| name | string | - |
| type | string | - |
| uri | string | - |
| width(optional) | number | null | - |
Type: Pick<PackagerAsset, 'httpServerLocation' | 'name' | 'hash' | 'type' | 'scales' | 'width' | 'height'> extended by:
| Property | Type | Description |
|---|---|---|
| fileHashes(optional) | string[] | - |
| fileUris(optional) | string[] | - |
| uri(optional) | string | - |