首页 > 解决方案 > 如何在 react native expo 中访问相机应用程序中拍摄的照片?

问题描述

我用 expo 相机模块制作了一个相机应用程序,我现在正试图访问这个应用程序中的特定相册,就像它发生在移动原生相机应用程序中一样。我尝试按照我看到的一些模糊说明创建一个类似的函数,但我不知道这是正确的路径。

//trying to access photos taken through the square

const albumName = "Gryffindor";
async function seePhotos() {
  const getTakenPhotos = await MediaLibrary.getAssetsAsync({
    first: 20,
    album: albumName,
    sortBy: ["creationTime"],
  });
  console.log(getTakenPhotos.assets);
  return MediaLibrary.getAssetsAsync.asset.uri;
}

<View style={{ flex: 1, alignItems: "center" }}>
  <Icon name="square" size={50} color="white" onPress={seePhotos} />
</View>;

标签: javascriptreactjsreact-nativeexpoexpo-camera

解决方案


用这个替换上面的代码

const albumName = "Gryffindor";

const seePhotos = async () => {
  try {
    const getTakenPhotos = await MediaLibrary.getAssetsAsync({
      first: 20,
      album: albumName,
      sortBy: MediaLibrary.SortBy.creationTime, // Should be written like this
    });
    console.log(getTakenPhotos.assets); // Check Your console
  } catch (error) {
    console.log(error); // In case of any error
  }
};

推荐阅读