世博会反应本机相机不起作用

时间:2017-11-22 07:21:38

标签: react-native camera expo

我使用here的基本代码让相机正常工作。唯一的区别是我没有制作一个独立的相机应用程序,而是将代码实现到其他应用程序(从应用程序内的按钮打开相机)

我正在严格遵守代码,但相机不会拍照并保存。甚至有警告说

[Unhandled Promise rejection: Error: TypeError: expected dynamic type `string', but had type `object']

我重新检查代码,我很确定它来自这部分

takePicture = async function() {
if (this.camera) {
  this.camera.takePictureAsync().then(data => {
    FileSystem.moveAsync({
      from: data,
      to: `${FileSystem.documentDirectory}photos/Photo_${this.state.photoId}.jpg`,
    }).then(() => {
      this.setState({
        photoId: this.state.photoId + 1,
      });
      Vibration.vibrate();
    });
  });
}
  };

快照按钮没有拍照。

我做了一个控制台日志,并找到了takePictureAsync()

的以下内容
if (this.camera) {
  this.camera.takePictureAsync().then(data => console.log(data));
}

这是结果

Object {
  "height": 1920,
  "uri":"file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540lily%252Fmotion/Camera/e501458d-f081-47b5-b7ac-6742f8e8d61e.jpg",
  "width": 1080,
}

这是警告expected dynamic type 'string', but had type 'object']中说的原因吗?以及应用程序没有拍照的可能原因?

如果是,我应该如何解决这个问题?

感谢您的帮助。

我的package.json

"expo": "^22.0.4",
"native-base": "^2.3.3",
"react": "16.0.0-beta.5",
"react-native": "https://github.com/expo/react-native/archive/sdk-22.0.1.tar.gz"

1 个答案:

答案 0 :(得分:-1)

看起来你确实拍了照片!现在你只需要将它保存在某个地方......

FileSystem.moveAsync({
      from: data,
      to: `${FileSystem.documentDirectory}photos/Photo_${this.state.photoId}.jpg`,
    }).then(() => {
      this.setState({
        photoId: this.state.photoId + 1,
      });
      Vibration.vibrate();
    });

您提供的代码使用FileSystem将照片保存在应用程序内部。您的照片将保存为以下内容:{documentsDirectory}/photos/Photo_{0}.jpg

如果您想将图像保存到相机胶卷,则应使用React Native CameraRoll API

您将使用静态方法await CameraRoll.saveToCameraRoll(photoUri)

相关问题