Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native save base64 image to Album

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
  • React-Native-Fetch-Blob - https://github.com/wkh237/react-native-fetch-blob
    still looking into it
  • React-Native-fs - https://github.com/itinance/react-native-fs
    I am trying this now
  • There are few npm modules with very little Github star (<10)

the React-Native-Fetch-Blob maintainer gone missing, so no one answering Github Issue, createFile from React-Native-Fetch-Blob Document not working as expected(not saving image into album)

  import fetch_blob from 'react-native-fetch-blob';

  // json.qr variable are return from API

  const fs = fetch_blob.fs
  const base64 = fetch_blob.base64
  const dirs = fetch_blob.fs.dirs

  const file_path = dirs.DCIMDir + "/some.jpg"
  const base64_img = base64.encode(json.qr)

  fs.createFile(file_path, base64_img, 'base64')
  .then((rep) => { 
    alert(JSON.stringify(rep));
  })
  .catch((error) => {
    alert(JSON.stringify(error));
  });

Anyone deal with this problem before?

How to save a base64 encode Image string to User album? (as a jpg or png file)

because I fetch an API with no CORS header,
I can't debug it in Debug JS Remotely
Chrome would stop that request from happening,

I have to run that on my Android Phone to make it work
(no CORS control on real phone)

I am planing use Clipboard save base64 string,
and hardcode it in my code,
to debug what's wrong with react-native-fetch-blob createFile API

like image 276
NamNamNam Avatar asked Jan 07 '18 04:01

NamNamNam


People also ask

How convert base64 to image in react native?

React Native allows image conversion in your app using the Image component. However, you cannot use this component to convert base64 images. In fact, you cannot use the base64 format for images in React Native without a specific API.

How do I open base64 PDF in react native?

You can use react-native-pdf package (https://www.npmjs.com/package/react-native-pdf). If you want to show the pdf in your app , this package would be quite helpful as it supports loading PDFs from base64 string for both ios and android .


2 Answers

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.

like image 190
Akshay I Avatar answered Sep 28 '22 15:09

Akshay I


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

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/

like image 35
NamNamNam Avatar answered Sep 28 '22 15:09

NamNamNam