我正在开发一个应用程序,其中我使用react-native-image-picker拍照并上传到AWS服务器。上传完一次后,我想在Android设备中(即保存的文件夹中)加密那些拍摄的图像。有可能吗?如果是,如何使用本机操作? 我的代码是 我能够捕获图像并将其上传到AWS s3。
import React, { Component } from "react";
import {
Platform,
StyleSheet,
Alert,
Text,
TouchableOpacity,
View,
Picker,
Animated,
Easing,
Image
} from "react-native";
import ImagePicker from "react-native-image-picker";
import { RNS3 } from "react-native-aws3";
export default class SecondScreen extends Component<Props> {
constructor(props) {
super(props);
this.state = {
file: "",
saveImages: []
};
}
takePic() {
ImagePicker.launchCamera({}, responce => {
const file = {
uri: responce.uri,
name: responce.fileName,
method: "POST",
path: responce.path,
type: responce.type,
notification: {
enabled: true
}
};
this.state.saveImages.push(file);
});
}
_upload = saveImages => {
const config = {
keyPrefix: "uploads/",
bucket: "s3merahkee",
region: "us-east-2",
accessKey: "***",
secretKey: "***",
successActionStatus: 201
};
this.state.saveImages.map(image => {
RNS3.put(image, config).then(responce => {
console.log(saveImages);
});
});
};
render() {
return (
<View style={styles.container}>
<View style={styles.Camera}>
<TouchableOpacity onPress={this.takePic.bind(this)}>
<Text>Take Picture</Text>
</TouchableOpacity>
</View>
<View style={styles.Send}>
<TouchableOpacity onPress={() => this._upload()}>
<Text>Send</Text>
</TouchableOpacity>
</View>
</View>
);
}
}
我在这里使用两种方法,一种用于捕获图像,一旦用户单击将其发送以将文件上传到AWS s3。
我希望我可以加密图像。如果可以,请告诉我如何实现它。或者,如果不可能的话,建议我以其他方式做到这一点。 (例如:删除等。)
答案 0 :(得分:0)
我通过将图像转换为base64来完成此操作。使用react-native-fs库,我能够实现这一点。在这里,一旦捕获了图像,便将它们转换为base64字符串,并从文件夹中删除真实图像。
我的代码是
takePic = () => {
// create a path you want to write to
var pictureFolder = RNFetchBlob.fs.dirs.SDCardDir+'/Schoolapp';
ImagePicker.launchCamera(options,(responce)=>{
const file ={
uri : responce.uri,
name : responce.fileName,
method: 'POST',
path : responce.path,
type : responce.type,
notification: {
enabled: true
}
}
//convert image into base64
const base64 = RNFS.writeFile(responce.uri, responce.data);
return base64;
//delete the original image
RNFS.unlink(responce.path)
.then(() => {
console.log('deleted');
RNFS.scanFile(responce.path)
.then(() => {
console.log('scanned');
})
.catch(err => {
console.log(err);
});
})
.catch((err) => {
console.log(err);
})
});
}