【React Native】安卓系統保存圖片到系統相冊(實例+錯誤解決)

  一、安裝第三方庫並引入

import CameraRoll from '@react-native-community/cameraroll';
import RNFS from 'react-native-fs';

   Github參考連接:CameraRoll  RNFSreact

  二、添加權限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

  三、在引用組件中引入如下代碼

  不然會報"Error:Permission denied"錯誤,用來獲取內存空間的讀寫權限。此處須要在頂部引入PermissionsAndroidandroid

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;
        }
    };
相關文章
相關標籤/搜索