将照片存储/移动到手机中的单独应用程序文件夹



我正在构建一个 react-native 应用程序,它可以拍照或从图库中选择照片,照片应该移动到手机中名为 {appName} 文件夹的单独文件夹中。我将所有图像存储在本地,因此将图像存储在文件夹中至关重要。 例如:当您将照片上传到Instagram或Whatsapp时,该应用程序将在手机中创建一个名为Instagram/Whatsapp的单独文件夹,并将其所有照片存储在该文件夹中。

我正在使用 react-native 来构建应用程序,并使用图像选择器来拍照或从图库中选择照片。

我目前正在尝试iPhone模拟器,但我希望它可以在Iphone和Android上运行。

我尝试使用"react-native-fs"和"react-native-fetch-blob"。但它无法正常工作,

const dirPictures = `${RNFS.DocumentDirectoryPath}/hazelnut`;
//const dirPictures = `${RNFS.PicturesDirectoryPath}/hazelnut`;
const newImageName = `${moment().format('DDMMYY_HHmmSSS')}.jpg`;
const newFilepath = `${dirPictures}/${newImageName}`;
const imageMoved = await this.moveAttachment(this.state.image.uri, newFilepath);
moveAttachment = async (filePath, newFilepath) => {
return new Promise((resolve, reject) => {
RNFS.mkdir(dirPictures)
.then( () => {
RNFS.moveFile(filePath, newFilepath)
.then(() => {
console.log('FILE MOVED', filePath, newFilepath);
resolve(true);
})
}) 
.catch(err => {
console.log('mkdir error', err);
reject(err);
});
});
};

图像被移动,但这不是我想要的行为。使用上面的代码,将其移动到文档目录。但我想把它移到照片目录中。如果我进入照片,我应该能够看到包含所有图像的应用程序文件夹。

如果我使用 const dirPictures =${RNFS.PicturesDirectoryPath}/hazelnut,那么我得到 您无权将文件"榛子"保存在文件夹"未定义"中。

我认为你应该使用这个:

const dirPictures = `${RNFS.DocumentDirectoryPath}/hazelnut`;

对于安卓

而这个:

const dirPictures = `${RNFS.LibraryDirectoryPath}/hazelnut`;

对于 iOS

最新更新