从 React-native cameraRoll 库中检索视频的 fileSize 和 playableDuration
Posted
技术标签:
【中文标题】从 React-native cameraRoll 库中检索视频的 fileSize 和 playableDuration【英文标题】:Retrieve fileSize and playableDuration of Video from React-native cameraRoll library 【发布时间】:2020-07-15 22:32:28 【问题描述】:我正在尝试使用适用于 React 本机的 cameraRoll 库打印每个视频的总持续时间和视频大小,但它对所有视频都显示为空。 这可以修复吗,所以我可以显示每个视频的总时长和视频大小。
CameraRoll 库链接https://github.com/react-native-community/react-native-cameraroll
export default class camera extends Component
constructor(props)
super(props);
this.state =
data:''
;
async componentDidMount()
if (Platform.OS === 'android')
const result = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
title: 'Permission Explanation',
message: 'ReactNativeForYou would like to access your photos!',
,
);
if (result !== 'granted')
console.log('Access to pictures was denied');
return;
CameraRoll.getPhotos(
first: 3,
assetType: 'Videos',
)
.then(res =>
this.setState(
data: res.edges,
);
)
.catch((error) =>
console.log(error);
);
render()
return (
<View style=flex: 1, flexDirection: 'row'>
<FlatList
data=this.state.data
numColumns=1
renderItem=( item ) =>
<View
style= flexDirection: 'row',
>
<Video
ref=(ref) =>
this.player = ref
source= uri: item.node.image.uri
onBuffer=this.onBuffer
onError=this.videoError
resizeMode='cover'
repeat=true
muted=true
playWhenInactive=true
style=
width: '30%',
height: 80,
marginLeft:10,
marginTop:10,
borderRadius:10,
/>
<Text
style=textAlign: 'center', alignSelf: 'stretch', textAlignVertical: 'center',
paddingLeft: 30, overflow: "hidden",
>JSON.stringify(item.node.image.fileSize) // this line should have displayed the video file size
</Text>
<Text
style=textAlign: 'center', alignSelf: 'stretch', textAlignVertical: 'center',
paddingLeft: 30, overflow: "hidden",
>item.node.image.playableDuration //This line should have displayed the video duration
</Text>
</View>
/>
</View>
);
这是我的完整代码。
【问题讨论】:
【参考方案1】:CameraRoll.getPhotos(
first: 3,
assetType: 'Videos',
includes: ['fileSize', 'playableDuration']
);
【讨论】:
【参考方案2】:这可能是由于 includeFileName 和 includeFileSize 变量为 false。 因此,要包含它们,请在以下文件路径中进行更改:node_modules/@react-native-community/cameraroll/android/src/main/java/com/reactnativecommunity/cameraroll/CameraRollModule.java
变化:
// if (includeFilename)
// File file = new File(media.getString(dataIndex));
// String strFileName = file.getName();
// image.putString("filename", strFileName);
// else
// image.putNull("filename");
//
// if (includeFileSize)
// image.putDouble("fileSize", media.getLong(sizeIndex));
// else
// image.putNull("fileSize");
//
File file = new File(media.getString(dataIndex)); //changed line
String strFileName = file.getName(); //changed line
image.putString("filename", strFileName); //changed line
image.putDouble("fileSize", media.getLong(sizeIndex)); //changed line
if (includePlayableDuration || !isVideo)
return true;
【讨论】:
以上是关于从 React-native cameraRoll 库中检索视频的 fileSize 和 playableDuration的主要内容,如果未能解决你的问题,请参考以下文章
我应该在标签上提供啥 url 以保存到 CameraRoll react-native?
使用 React-Native 将 content:// URI 转换为 Android 的实际路径