从 React-native cameraRoll 库中检索视频的 fileSize 和 playableDuration
Retrieve fileSize and playableDuration of Video from React-native cameraRoll library
我正在尝试使用 React native 的 cameraRoll 库打印每个视频的总持续时间和视频大小,但它对所有视频都显示为空。
这可以修复吗,所以我可以显示每个视频的总持续时间和视频大小。
Link 用于 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>
);
}
}
这是我的完整代码。
这可能是因为 includeFileName 和 includeFileSize 变量为假。
因此,要包括它们,请在以下文件中进行更改 path: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;
}
CameraRoll.getPhotos({
first: 3,
assetType: 'Videos',
includes: ['fileSize', 'playableDuration']
});
我正在尝试使用 React native 的 cameraRoll 库打印每个视频的总持续时间和视频大小,但它对所有视频都显示为空。 这可以修复吗,所以我可以显示每个视频的总持续时间和视频大小。
Link 用于 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>
);
}
}
这是我的完整代码。
这可能是因为 includeFileName 和 includeFileSize 变量为假。 因此,要包括它们,请在以下文件中进行更改 path: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;
}
CameraRoll.getPhotos({
first: 3,
assetType: 'Videos',
includes: ['fileSize', 'playableDuration']
});