简体   繁体   English

EXPO 从相机拍摄后将照片保存在图片中

[英]EXPO save photo in Pictures after taking it from Camera

EXPO React Native, running the app on Expo Go on my android mobile. EXPO React Native,在 Expo Go 上运行我的 ZC31B32364CE19CA8FCD150A4188 手机上的应用程序。

I am trying to save photo taken by the Camera to Pictures folder on my Android device.我正在尝试将相机拍摄的照片保存到我的 Android 设备上的图片文件夹中。 It just not happening.它只是没有发生。 Can anyone help me figure out what is wrong?谁能帮我找出问题所在?

takePicture = async () => {
  const { uri } = await this.camera.takePictureAsync();
  console.log(‘uri’, uri);
  const asset = await MediaLibrary.createAssetAsync(uri);
  console.log(‘asset’, asset.filename);
  MediaLibrary.createAlbumAsync(‘Pictures’, asset)
  .then(() => {
    Alert.alert(‘Album created!’)
  })
  .catch(error => {
    Alert.alert(‘An Error Occurred!’)
  });
   this.setState({ photoId: this.state.photoId + 1 });
   Vibration.vibrate(); 
}

my console return the uri after photo taken but asset.file is not returned it is like nothing is happening there.我的控制台在拍摄照片后返回 uri 但asset.file 没有返回它就像那里什么都没有发生一样。 any help is appreciated.任何帮助表示赞赏。

I dumped medialibrary for imagepicker and it worked with this code我为 imagepicker 转储了 medialibrary,它与这段代码一起工作

  _takePhoto = async () => {
    const {
      status: cameraPerm
    } = await Permissions.askAsync(Permissions.CAMERA);

    const {
      status: cameraRollPerm
    } = await Permissions.askAsync(Permissions.CAMERA_ROLL);

    // only if user allows permission to camera AND camera roll
    if (cameraPerm === 'granted' && cameraRollPerm === 'granted') {
      let pickerResult = await ImagePicker.launchCameraAsync({
        mediaTypes: ImagePicker.MediaTypeOptions.Images,
        allowsEditing: false,
        aspect: [4, 3],
        quality: 0.2,
      });

      this._handleImagePicked(pickerResult);
    }
  };

You can use this function of expo-media-library but you can't specify the folder.您可以使用 expo-media-library 的 function 但不能指定文件夹。

await MediaLibrary.saveToLibraryAsync(uri);

I'm thinking of using this function below to move the image to a specific folder.我正在考虑使用下面的 function 将图像移动到特定文件夹。

MediaLibrary.addAssetsToAlbumAsync(assets, album, copyAssets)

You can check this url Expo Media Library您可以查看此 url Expo 媒体库

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM