简体   繁体   English

将捕获的图像从相机保存到自定义文件夹反应本地

[英]Save captured image from camera to custom folder react native

So I am creating a simple camera app using react-native. 所以我正在使用react-native创建一个简单的相机应用程序。 For camera component I am using react-native-camera package. 对于相机组件,我正在使用react-native-camera软件包。 So far I am able to capture a photo and save it into DCIM folder using react-native CameraRoll Api CameraRoll.saveToCameraRoll , but I want to save it in another folder like DCIM/CameraApp or in Pictures folder. 到目前为止,我能够使用react-native CameraRoll Api CameraRoll.saveToCameraRoll捕获照片并将其保存到DCIM文件夹中,但是我想将其保存在另一个文件夹中,例如DCIM/CameraApp或在Pictures文件夹中。 To achieve this I am using RNFetchBlob package. 为此,我使用了RNFetchBlob包。 I have tried a few ways to create a new file from the uri that is returned by this.camera.takePictureAsync but it throws error that the file does not exists. 我尝试了几种方法,可以从this.camera.takePictureAsync返回的uri创建新文件,但是会引发错误,指出该文件不存在。 But if I pass that uri to CameraRoll.saveToCameraRoll it saves it in DCIM folder. 但是,如果我将该uri传递给CameraRoll.saveToCameraRoll它将保存在DCIM文件夹中。

My current code: 我当前的代码:

    const options = { quality: 0.5 };

    const data = await this.camera.takePictureAsync(options)
    const { uri, height, width } = data;
    this.setState({ uri, height, width });

    CameraRoll.saveToCameraRoll(data.uri, 'photo') // it saves it into DCIM
    .then( uri => {
        // tried this too
        //RNFetchBlob.fs.createFile(`${RNFetchBlob.fs.dirs.DCIMDir}/newfile.png`, uri, 'uri'); // throws error that file does not exist
        RNFetchBlob.fs.cp(`${RNFetchBlob.fs.dirs.DCIMDir}`, `${RNFetchBlob.fs.dirs.DCIMDir}/CameraApp/p.png`);  
        console.warn('uri:', uri)
    })
    .catch( err => console.warn('err:', err));
    console.warn(data);

I have tried few weird ways just to save file where I want but in vain. 我尝试了几种奇怪的方法,只是将文件保存在我想要的位置,但是徒劳。 I hope I have cleared my intentions but please let me know if you need more info. 我希望我已经清除了我的意图,但是如果您需要更多信息,请告诉我。 Any suggestions are welcome. 欢迎任何建议。

**I am able to create the directory,but i failed to move the images to that directory,however you can check if it helps you.!! **我能够创建目录,但是我无法将图像移动到该目录,但是您可以检查它是否对您有帮助!! ** **

import React from 'react';
//import react in our code. 
import { StyleSheet, Text, View, 
Alert, ActivityIndicator, PermissionsAndroid } from 'react-native';
//import all the basic components we are going to use.
import { CameraKitCameraScreen } from 'react-native-camera-kit';
//import CameraKitCameraScreen we are going to use.
import RNFetchBlob from 'react-native-fetch-blob'

export default class App extends React.Component {
state = {isPermitted:false}
constructor(props) {
super(props);
var that=this;
async function requestCameraPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA,{
'title': 'CameraExample App Camera Permission',
'message': 'CameraExample App needs access to your camera '
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
//If CAMERA Permission is granted
//Calling the WRITE_EXTERNAL_STORAGE permission function
requestExternalWritePermission();
} else {
alert("CAMERA permission denied");
}
} catch (err) {
alert("Camera permission err",err);
console.warn(err)
}
}
async function requestExternalWritePermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,{
'title': 'CameraExample App External Storage Write Permission',
'message': 'CameraExample App needs access to Storage data in your SD Card '
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
//If WRITE_EXTERNAL_STORAGE Permission is granted
//Calling the READ_EXTERNAL_STORAGE permission function
requestExternalReadPermission();
} else {
alert("WRITE_EXTERNAL_STORAGE permission denied");
}
} catch (err) {
alert("Write permission err",err);
console.warn(err)
}
}
async function requestExternalReadPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,{
'title': 'CameraExample App Read Storage Write Permission',
'message': 'CameraExample App needs access to your SD Card '
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
//If READ_EXTERNAL_STORAGE Permission is granted
//changing the state to re-render and open the camera 
//in place of activity indicator
that.setState({isPermitted:true})
} else {
alert("READ_EXTERNAL_STORAGE permission denied");
}
} catch (err) {
alert("Read permission err",err);
console.warn(err)
}
}
//Calling the camera permission function
requestCameraPermission();
}
onBottomButtonPressed(event) {

if (event.type) {
//const captureImages = JSON.stringify(event.captureImages);

if (event.type == "capture") {

const pictureFolder = RNFetchBlob.fs.dirs.SDCardDir+'/Optimize/';
const captureImageLength = event.captureImages.length;
RNFetchBlob.fs.exists(pictureFolder).then((exists)=>{
if(exists){
RNFetchBlob.fs.isDir(pictureFolder).then((isDir)=>{
if(isDir){

RNFetchBlob.fs.mv(event.captureImages[0].uri, RNFetchBlob.fs.dirs.SDCardDir+'/Optimize/').then(() => {
alert('Image Moved');
}).catch((e)=>{ alert("FAILED:= "+e.message) });
}else{
alert('Some Error Happened');
}
}).catch((e)=>{ alert("Checking Directory Error : "+e.message); });
}else{
RNFetchBlob.fs.mkdir(pictureFolder).then(()=>{
alert('DIRECTORY CREATED');
}).catch((e)=>{ alert("Directory Creating Error : "+e.message); });
}
});
}
}
}
render() {
if(this.state.isPermitted){
return (
<CameraKitCameraScreen
// Buttons to perform action done and cancel
actions={{ rightButtonText: 'Done', leftButtonText: 'Cancel' }}
onBottomButtonPressed={event => this.onBottomButtonPressed(event)}
flashImages={{
// Flash button images
on: require('./assets/flashon.png'),
off: require('./assets/flashoff.png'),
auto: require('./assets/flashauto.png'),
}}
cameraFlipImage={require('./assets/flip.png')}
captureButtonImage={require('./assets/capture.png')}
/>
);
}else{
return ( 
<ActivityIndicator />
)
}
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用android中的自定义相机将捕获的图像保存在文件夹中 - Can't save captured image in folder using custom camera in android 将ImageView(从相机捕获)中的图像保存到内部存储器的自定义文件夹中 - Save the image from an ImageView(captured from camera) in my custom defined folder in the INTERNAL MEMORY 如何将相机拍摄的图像保存在特定文件夹中 - How to save image captured with camera in specific folder 反应本机和rn-fetch-blob将捕获的图像从图片移动到在Android中创建的自定义文件夹 - React native and rn-fetch-blob move captured image from Pictures to custom folder created in Android 如何在 android 中保存从相机拍摄的图像 - How to save captured image from camera in android 如何将图片从 React Native Camera Roll 保存到特定文件夹 - How to save a Picture to a specific folder from React Native Camera Roll Android将自定义相机中的图像保存到SD卡上的自定义文件夹中 - Android Save Image from Custom Camera to Custom Folder on SD Card 如何在Android中使用自定义文件名保存相机拍摄的图像? - How to save camera captured image with custom file name in Android? 保存相机拍摄的图像JPG - Save image JPG captured by the camera Android在自定义文件夹中保存相机图像 - Android Save Camera Image in Custom Folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM