我正在做一个 React Native 项目。我需要将捕获的图像存储在自定义文件夹中。
我为此使用了 react native fs 库。我能够在所需目录中创建图像,但我无法在我的 iphone 文件目录中看到这些图像。
这是我用来存储图像的代码。
async moveAttachment(capturedImagePath) {
$filePathDir = `${RNFS.DocumentDirectoryPath}/myapp/myfilename`;
$filePath = `${$filePathDir }/myfilename.png`;
return new Promise((resolve, reject) => {
RNFS.mkdir(filePathDir)
.then(() => {
RNFS.moveFile(capturedImagePath, filePath )
.then(() => resolve(dirPictures))
.catch(error => reject(error));
})
.catch(err => reject(err));
});
}
Run Code Online (Sandbox Code Playgroud)
我能够在模拟器的文档目录中看到图像,但无法在 iPhone > 文件目录中看到。
请帮我解决这个问题。
我需要在按下按钮时下载一组图像。目前,我正在使用以下方式这样做react-native-fs
:
const downloadImageItem = async (imgUrl, id) => {
const path = `${RNFS.DocumentDirectoryPath}/${id}.jpg`;
RNFS.downloadFile({
fromUrl: imgUrl,
toFile: path,
});
};
Run Code Online (Sandbox Code Playgroud)
const downloadImages = async (items) => {
for (const item of items) {
if (item.images.length) {
await downloadImageItem(item.images[0].thumb, item.images[0].id);
}
}
return Promise.resolve();
};
Run Code Online (Sandbox Code Playgroud)
从我的减速器中为 3 种类型的项目调用函数:
await downloadImages(items_one);
await downloadImages(items_two);
await downloadImages(items_three);
Run Code Online (Sandbox Code Playgroud)
我的问题是有时我会收到一条错误消息: 过多的待处理回调数:501
有没有更好的方法来做同样的事情,这样错误就不会出现?
async-await reactjs react-native react-redux react-native-fs
我尝试了创建文件的代码,我创建了两个文件,那就是
// require the module
var RNFS = require('react-native-fs');
// create a path you want to write to
var path = RNFS.DocumentDirectoryPath + '/test.txt';
// write the file
RNFS.writeFile(path, 'Lorem ipsum dolor sit amet', 'utf8')
.then((success) => {
console.log('FILE WRITTEN!');
})
.catch((err) => {
console.log(err.message);
});
Run Code Online (Sandbox Code Playgroud)
我用该代码创建了两个文件。但我看不到实际创建的文件。它究竟保存在哪里?
我正在尝试向我的 RN 应用程序添加功能,以允许用户在他们手机的文件系统中创建一个新目录。
我尝试编写代码,以便该函数在路径 /storage/emulated/0/AppName/NewFolder 中创建一个目录,因为 /storage/emulated/0 与我用来存储用户的其他应用程序使用的路径相同数据(例如录音应用程序)
makeDirectory = () => {
const { currentFolder, units } = this.props;
const directoryName = 'New Folder'
const currentDirectory = units
const absolutePath = `/storage/emulated/0/MyApp/${currentDirectory}`
RNFS.mkdir(absolutePath)
.then((result) => {
console.log('result', result)
})
.catch((err) => {
console.warn('err', err)
})
}
Run Code Online (Sandbox Code Playgroud)
但是,这只是给我一个错误:无法创建目录。我觉得我在这里遗漏了一些东西,不应该将这样的文件保存到手机系统中。
我的最终目标是让应用程序拥有自己的文件夹系统,该系统将被镜像到 /storage/emulated/0/MyApp/home
在我的 Android 反应本机应用程序中,我将 jpg 文件从缓存文件夹移动到 RNFS.DocumentDirectoryPath,我在其中创建了一个“图像”文件夹。我无法渲染这些图像:
在反应类中:
state = {source:null}
async componentDidMount() {
async loadFile ( path ){
await this.setState({source:{uri:path}})
}
const path = RNFS.DocumentDirectoryPath+'/images/d88b102c-d4c6-4dc1-9a4c-f2a0e599ddbf.jpg'
await RNFS.exists(path).then( exists => {
if(exists){
this.loadFile(path);
}
}
renderItem = ({ item }) => (
<View key={item.Id} >
<View>
<TouchableOpacity onPress={() => this.onPressItem(item)}>
<Text>{item.cardName}</Text>
<Image
source={this.state.source}
style={{ width:'auto', height: 55 }}
/>
</TouchableOpacity>
</View>
</View>
);
Run Code Online (Sandbox Code Playgroud)
如果我将图像转换为 base64,图像就会存在,它会正确渲染。
我正在使用react-native-fs构建一个类似应用程序的媒体播放器,但我无法访问项目文件夹之外的目录,我添加了以下权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)
我还使用以下代码检查读写权限:
PermissionsAndroid.check(
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
).then(granted => {
if (!granted) {
PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
]).then(result => {
console.log(result);
});
}
});
Run Code Online (Sandbox Code Playgroud)
RNFS.readDir() 抛出 null 错误:
const path = RNFS.ExternalStorageDirectoryPath;
RNFS.readDir(path).then(result => {
console.log('GOT RESULT', result);
});
Run Code Online (Sandbox Code Playgroud)
抛出的错误是:
[Error: Attempt to get length of null array]
Run Code Online (Sandbox Code Playgroud) 我正在使用react-native-fetch-blob库中的文件系统API。
我从服务器请求图像,接收 Base64,然后将其保存到 android fs 中的 Pictures 目录。
我这样保存图像:
var RNFetchBlob = require('react-native-fetch-blob').default;
const PictureDir = RNFetchBlob.fs.dirs.PictureDir;
getImageAttachment: function(uri_attachment, filename_attachment, mimetype_attachment) {
return new Promise((RESOLVE, REJECT) => {
// Fetch attachment
RNFetchBlob.fetch('GET', config.apiRoot+'/app/'+uri_attachment)
.then((response) => {
let base64Str = response.data;
let imageLocation = PictureDir+'/'+filename_attachment;
//Save image
fs.writeFile(imageLocation, base64Str, 'base64');
console.log("FILE CREATED!!")
}).catch((error) => {
// error handling
console.log("Error:", error)
});
},
Run Code Online (Sandbox Code Playgroud)
问题
图像保存成功,但当我打开 Android 的图库时,它不可见。但是当我关闭并打开 Android 模拟器时,它就会在图库中可见。
问题
如何在不重启模拟器的情况下查看Android图库中的图片?
创建文件后,我将其添加到上述方法中:
RNFetchBlob.fs.scanFile([ { path : imageLocation, …
Run Code Online (Sandbox Code Playgroud) 我正在使用 react-native-fs 进行文件系统访问。但是我无法访问ios中的文件。
下面是代码:
RNFS.readDir(RNFS.MainBundlePath)
.then((result) => {
console.log('Got result', result);
return Promise.all([RNFS.stat(result[0].path), result[0].path]);
})
.then((statResult) => {
console.log('stat result',statResult);
if(statResult[0].isFile()){
return RNFS.readFile(statResult[1], 'utf8');
}
return 'no file';
})
.then((contents) => {
console.log('Got Contents',contents);
})
.catch((err) => {
console.log(err.message, err.code);
})
Run Code Online (Sandbox Code Playgroud)
我正在获取 MainBundlePath 的路径文件,但没有获取本地存储的任何其他文件/文件夹。
我的应用程序中有本地文件,其内容如下:
<Image source={require("../../img/pic1.jpg") />
如何使用RNFS.readFile(filepath, "base64");
使用读取此文件RNFS.readFile("../../img/pic1.jpg", "base64");
给我错误“无此文件”
我正在使用react-native版本0.69,我想读取存储在我的react native项目中的资产中的文件。在中使用以下语法App.js
import fs from 'react-native-fs';
我已经安装了“react-native-fs”并按照我在互联网上找到的所有链接进行操作,但在运行应用程序时仍然遇到以下错误。
ERROR TypeError: null is not an object (evaluating 'RNFSManager.RNFSFileTypeRegular')
ERROR Invariant Violation: "main" has not been registered. This can happen if:
* Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current
project.
* A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called.
Run Code Online (Sandbox Code Playgroud)
我尝试运行,react-native link react-nativs-fs
但如官方文档中所述,react-native >= 60 中的链接支持已被删除,我无法使其正常工作,任何帮助将不胜感激。我在 stackoverflow 上看到了类似的问题,但没有人给出 React …
react-native ×10
react-native-fs ×10
android ×2
reactjs ×2
async-await ×1
image ×1
react-redux ×1