React-native-android - How to save an image in the Android file system and view Gallery in your phone,

Is it possible to save an image in the local Android file system so that it can be viewed from the Gallery phone and in a folder?

I found this react-native-fs library , but after studying the documentation and working with the example, I'm still not sure if this is possible.

thank

+4
source share
2 answers

For those who have the same problem, here is a solution.

Decision

API react-native-fetch-blob . , , , "response-native-fs".

, base64, Pictures android fs.

:

var RNFetchBlob = require('react-native-fetch-blob').default;

const PictureDir = RNFetchBlob.fs.dirs.PictureDir;

getImageAttachment: function(uri_attachment, filename_attachment, mimetype_attachment) {

   return new Promise((RESOLVE, REJECT) => {

   // Fetch attachment
   RNFetchBlob.fetch('GET', config.apiRoot+'/app/'+uri_attachment)
   .then((response) => {

     let base64Str = response.data;

     let imageLocation = PictureDir+'/'+filename_attachment;

     //Save image
     fs.writeFile(imageLocation, base64Str, 'base64');
     console.log("FILE CREATED!!")

     RNFetchBlob.fs.scanFile([ { path : imageLocation, mime : mimetype_attachment } ])
     .then(() => {
       console.log("scan file success")
     })
     .catch((err) => {
       console.log("scan file error")
     })

    }).catch((error) => {
    // error handling
    console.log("Error:", error)
  });
},

, , , , .

RNFetchBlob.fs.scanFile([ { path : imageLocation, mime : mimetype_attachment } ])
.then(() => {
  console.log("scan file success")
})
.catch((err) => {
  console.log("scan file error")
})

!

+10

react-native-fs. PicturesDirectoryPath, README ; , "". , , ,

const myAlbumPath = RNFS.PicturesDirectoryPath + '/My Album'

RNFS.mkdir(myAlbumPath)
  .then(/* write/copy/download your image file into myAlbumPath here */)

, . , !

+7

Source: https://habr.com/ru/post/1648745/


All Articles