实际上,我在我的React Native Expo应用程序的assets文件夹中添加了一些图像。我希望当用户打开我的应用程序时,他们可以在物理设备上下载或保存这些图像。我试过很多次了,但我没有找到解决办法。我尝试了下面的代码,它在Expo Go应用程序上运行良好,但当我安装APK并打开应用程序时,它不会下载图像。你能提出一个解决方案吗?
我试过这个,它在expo go应用程序上工作,但在物理设备上不工作。
import React from 'react';
import {View, Text, Button, Image} from 'react-native'
import * as MediaLibrary from 'expo-media-library';
import {Asset} from 'expo-asset';
import { useRoute } from '@react-navigation/native';
const Test = () => {
//Example Image Url = require('../assets/nk/myimage.jpg')
const route = useRoute();
const saveFileToMediaLibrary = async () => {
try {
const asset = Asset.fromModule(route.params.u);
// Download the asset to the local file system
await asset.downloadAsync();
// Get the local URI of the downloaded file
const fileUri = asset.localUri;
// File saved successfully
console.log('File saved to media library:', fileUri);
// Save the file to the media library
const perm = await MediaLibrary.requestPermissionsAsync();
if (perm.status != "granted") {
return;
}
try {
const asset = await MediaLibrary.createAssetAsync(fileUri);
console.log(asset);
const album = await MediaLibrary.getAlbumAsync("My App Images");
if (album == null) {
await MediaLibrary.createAlbumAsync("My App Images", asset, false);
} else {
await MediaLibrary.addAssetsToAlbumAsync([asset], album, false);
}
} catch (e) {
alert("Something Went Wrong..."+ e);
}
} catch (error) {
alert('Error to save file in media library', fileUri);
}
};
return (
<View>
<Button title="Download File" onPress={()=>{saveFileToMediaLibrary()}} />
<Image
source={route.params.u} style={{width:100,height:100}}
/>
</View>
);
};
export default Test;
1条答案
按热度按时间2ledvvac1#
我用下面的代码复制sqlite数据库从资产文件夹沿着项目,也许你可以尝试与图像
参考:https://docs.expo.dev/versions/latest/sdk/sqlite/