React Native save base64 image to Album

前端 未结 5 1762
没有蜡笔的小新
没有蜡笔的小新 2021-02-05 21:22

Third Party API return a \"QR code image\" in base64 encode,
I need save that image to User\'s Album.

  • CamerRoll - not support saving base64 image to album <
相关标签:
5条回答
  • 2021-02-05 21:30

    I solve the problem,
    turn out I forgot data:image/png;base64, at beginning of the string.

    I remove it with following code

      // json.qr is base64 string
      var image_data = json.qr.split('data:image/png;base64,');
      image_data = image_data[1];
    

    and then save the image file

      import fetch_blob from 'react-native-fetch-blob';
      import RNFS from 'react-native-fs';
    
      const fs = fetch_blob.fs
      const dirs = fetch_blob.fs.dirs      
      const file_path = dirs.DCIMDir + "/bigjpg.png"
    
      // json.qr is base64 string "data:image/png;base64,..."
    
      var image_data = json.qr.split('data:image/png;base64,');
      image_data = image_data[1];
    
      RNFS.writeFile(file_path, image_data, 'base64')
      .catch((error) => {
        alert(JSON.stringify(error));
      });
    

    I wrote a blog about this

    http://1c7.me/react-native-save-base64-image-to-album/

    0 讨论(0)
  • 2021-02-05 21:37

    I got this worked in following example

    import RNFetchBlob from 'rn-fetch-blob';
    import Permissions from 'react-native-permissions';
    
     takeSnapshot = async () => {
        const currentStatus = await Permissions.check('storage');
    
        if (currentStatus !== 'authorized') {
          const status = await Permissions.request('storage');
    
          if (status !== 'authorized') {
            return false;
          }
        }
    
        // put here your base64
        const base64 = '';
    
        const path = `${RNFetchBlob.fs.dirs.DCIMDir}/test11.png`;
    
        try {
          const data = await RNFetchBlob.fs.writeFile(path, base64, 'base64');
          console.log(data, 'data');
        } catch (error) {
          console.log(error.message);
        }
      };
    
    0 讨论(0)
  • 2021-02-05 21:47

    const path = `${RNFS.PicturesDirectoryPath}/My Album`;
    await RNFS.mkdir(path);
    return await fetch(uri)
       .then(res => res.blob())
       .then(image => {
          RNFetchBlob.fs.readFile(uri, "base64").then(data => {
             RNFS.appendFile(`${path}/${image.data.name}`, data, "base64").catch(
                err => {
                   console.log("error writing to android storage :", err);
                }
             );
          });
       });

    0 讨论(0)
  • 2021-02-05 21:53

    You can now use only react native fetch blob to achieve this.

    Simply replace RNFS.writeFile with

    RNFetchBlob.fs.writeFile(file_path, image_data, 'base64')
    

    If you wish to view file in native OS viewer you can simply put

                    if (isAndroid) {
                      RNFetchBlob.android.actionViewIntent(file_path, 'application/pdf');
                    } else {
                      RNFetchBlob.ios.previewDocument(file_path);
                    }
    
    0 讨论(0)
  • 2021-02-05 21:53

    Remove data:image/png;base64, in your base64 string

    var Base64Code = base64Image.split("data:image/png;base64,"); //base64Image is my image base64 string
    
    const dirs = RNFetchBlob.fs.dirs;
    
    var path = dirs.DCIMDir + "/image.png";
    
    RNFetchBlob.fs.writeFile(path, Base64Code[1], 'base64')
    .then((res) => {console.log("File : ", res)});
    

    And then I solved my problem.

    0 讨论(0)
提交回复
热议问题