我不明白为什么它会读取文件夹中除pdf格式文件之外的所有文件,有什么问题吗?还是我做的不对? 这是我的代码
const readFiles = async () => {
try {
const path =
Platform.OS === 'android'
? `${RNFS.ExternalStorageDirectoryPath}/Download`
: RNFS.DocumentDirectoryPath;
console.log('Reading files from:', path);
const files = await RNFS.readDir(path);
const pdfFiles = files.filter(file =>
file.name.toLowerCase().endsWith('.pdf'),
);
setPdfFiles(files);
console.log('PDF Files:', pdfFiles);
} catch (error) {
console.error('Error reading files:', error);
Alert.alert('Error reading files:', error.message);
}
};
如果有人帮忙我会很高兴)
import React, { useState, useEffect } from 'react';
import { View, Text, Button, Alert, Platform } from 'react-native';
import RNFS from 'react-native-fs';
import { PermissionsAndroid } from 'react-native';
const App = () => {
const [pdfFiles, setPdfFiles] = useState([]);
useEffect(() => {
// Request permissions on component mount for Android
if (Platform.OS === 'android') {
requestStoragePermission().then(permissionGranted => {
if (permissionGranted) {
readFiles();
} else {
Alert.alert('Permission Denied', 'Cannot read files without storage permission.');
}
});
} else {
readFiles();
}
}, []);
const requestStoragePermission = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
{
title: 'Storage Permission',
message: 'App needs access to your storage to read PDF files.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
}
);
return granted === PermissionsAndroid.RESULTS.GRANTED;
} catch (err) {
console.warn(err);
return false;
}
};
const readFiles = async () => {
try {
// Determine the correct path based on the platform
const path = Platform.OS === 'android'
? `${RNFS.ExternalStorageDirectoryPath}/Download`
: RNFS.DocumentDirectoryPath;
console.log('Reading files from:', path);
// Check if the directory exists
const directoryExists = await RNFS.exists(path);
if (!directoryExists) {
console.error('Directory does not exist:', path);
Alert.alert('Directory does not exist:', path);
return;
}
// Read the directory
const files = await RNFS.readDir(path);
console.log('Files found:', files);
// Filter for PDF files
const pdfFiles = files.filter(file =>
file.name.toLowerCase().endsWith('.pdf')
);
// Check if there are PDF files
if (pdfFiles.length === 0) {
console.log('No PDF files found');
Alert.alert('No PDF files found');
} else {
console.log('PDF Files:', pdfFiles);
setPdfFiles(pdfFiles);
}
} catch (error) {
console.error('Error reading files:', error);
Alert.alert('Error reading files:', error.message);
}
};
return (
<View style={{ padding: 20 }}>
<Text>PDF Files:</Text>
{pdfFiles.length > 0 ? (
pdfFiles.map((file, index) => (
<Text key={index}>{file.name}</Text>
))
) : (
<Text>No PDF files found.</Text>
)}
<Button title="Refresh" onPress={readFiles} />
</View>
);
};
export default App;
确保您在 AndroidManifest.xml 中拥有权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />