Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

I found this react-native-fs library but after studying the documentation and working through an example I am still unsure if it is possible.

Thanks

like image 580
Larney Avatar asked Jul 22 '16 08:07

Larney


People also ask

How do I save an image in Localstorage in React Native?

To store value in local storage in React Native, we can use AsyncStorage. to call AsyncStorage. setItem with the key and value to storage the entry with the key and value. Then we call AsyncStorage.

How do you display images from local assets images folder when working with react?

1.When using Create React App: Now, download the images that we will be using to display from HERE. If you save the file and load the application, you will see that, the image is not displayed but broken icon is displayed for the button with alt text.


2 Answers

For anyone having the same problem, here is the solution.

Solution

I am using the File System API from the react-native-fetch-blob library. This is because I tought it was way better documented and easier to understand than the 'react-native-fs' library.

I request an image from the server, receive a base64 and I then save it to the Pictures directory in the android fs.

I save the image like this:

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)
  });
},

The following code that is in the above method refreshes the Gallery otherwise the images would not display untill the phone is turned off and back on again.

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

Enjoy!

like image 87
Larney Avatar answered Oct 12 '22 06:10

Larney


You can absolutely do this with react-native-fs. There's a PicturesDirectoryPath constant which isn't mentioned in the README for the project; if you save a file into there it should appear in the Gallery app. If you want it to appear in your own album, just make a new directory in that folder and save the file into there, eg

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

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

I don't have full example code anymore sorry, because I ended storing images in my app's private cache directory instead. Hope this helps anyway!

like image 40
warby Avatar answered Oct 12 '22 04:10

warby