1、安裝第三方庫並引入 import CameraRoll from '@react-native-community/cameraroll'; import RNFS from 'react-native-fs'; Github參考鏈接:CameraRoll RNFS 2、添加許可權 <uses-p ...
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; } };