首页 > 解决方案 > 是否可以使用本机反应加密 android 设备的图像?

问题描述

我正在开发一个应用程序,我在其中使用 react-native-image-picker 拍照并将其上传到 AWS 服务器。一旦上传那些我想在Android设备中加密那些拍摄的图像,即在保存的文件夹中。有可能吗?如果是,我该如何使用 react-native 来做到这一点?我的代码是,我能够捕获图像并上传到 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。

我希望我可以加密图像如果可能的话告诉我如何实现它。或者,如果不可能的话,建议我用另一种方式来做。(例如:删除等)

标签: react-nativereact-native-android

解决方案


我已经使用将图像转换为 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);
        })

        });
    }

推荐阅读