android 如何获取本地视频列表并在React Native中以网格格式显示它们?

7fyelxc5  于 2024-01-04  发布在  Android
关注(0)|答案(2)|浏览(128)

我试图做一些类似于什么Instagram为他们选择的视频屏幕.其中可以上传的本地视频网格,它会种'预览'一个是目前选定的网格本身.
我正在使用react-native-community/cameraroll,这是我试图获得视频的代码。

  1. CameraRoll.getPhotos({first: 20, assetType: "All"})
  2. .then(r => this.setState({ videos: r.edges }))
  3. .catch((err) => {
  4. console.log('getVideosErr:' + err)
  5. })

字符串
我试图用我找到的一些示例代码来显示网格,并尝试使用:

  1. <ScrollView>
  2. {this.state.videos.map((p, i) => {
  3. return (
  4. <Video
  5. key={i}
  6. style={{
  7. width: 300,
  8. height: 100,
  9. }}
  10. source={{ uri: p.node.video.uri }}
  11. />
  12. ;
  13. })}


我在模拟器上有一个视频,但每次我尝试抓取视频时,都会出现一个错误,说“TypeError:Cannot read property 'uri' of undefined”。
不太确定现在的问题是什么,我遵循了cameraroll文档上的示例,但还没有任何运气。任何建议/示例将不胜感激。

vc9ivgsu

vc9ivgsu1#

使用react-native-media-helper
实施:

  1. import MediaHelper from 'react-native-media-helper'
  2. <MediaHelper
  3. numVideos={20} // for android
  4. media='Videos' // for ios
  5. num={20} // for ios
  6. onCancel={() => this.setState({visible: false})}
  7. onSelectedItem={(item) => alert(JSON.stringify(item))}
  8. />

字符串

ghhkc1vu

ghhkc1vu2#

这是因为节点对象没有任何video属性。视频文件也存储在image属性下。考虑将代码更改为this以摆脱null异常。

  1. <ScrollView>
  2. {this.state.videos.map((p, i) => {
  3. return (
  4. <Image
  5. key={i}
  6. style={{
  7. width: 300,
  8. height: 100,
  9. }}
  10. source={{uri: p.node.image.uri}}
  11. />
  12. })
  13. }

字符串
此外,由于您只需要视频,我建议将assetType指定为“Videos”

  1. CameraRoll.getPhotos({first: 20, assetType: "Videos"})
  2. .then(r => this.setState({ videos: r.edges }))
  3. .catch((err) => {
  4. console.log('getVideosErr:' + err)
  5. })

展开查看全部

相关问题