1、安装第三方库并引入
import CameraRoll from ‘@react-native-community/cameraroll‘; import RNFS from ‘react-native-fs‘;
Github参考链接:CameraRoll RNFS
2、添加权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
3、在引用组件中引入以下代码
否则会报"Error:Permission denied"错误,用来获取内存空间的读写权限。此处需要在顶部引入PermissionsAndroid
requestExternalStoragePermission = async () => { try { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, { title: ‘My App Storage Permission‘, message: ‘My App needs access to your storage ‘ + ‘so you can save your photos‘, }, ); return granted; } catch (err) { console.error(‘Failed to request permission ‘, err); return null; } };