Skip to content

Instantly share code, notes, and snippets.

@1c7
Last active January 17, 2018 14:26
Show Gist options
  • Save 1c7/ff020daa0b939737f3caa2ca0d1785be to your computer and use it in GitHub Desktop.
Save 1c7/ff020daa0b939737f3caa2ca0d1785be to your computer and use it in GitHub Desktop.
(React Native 0.51) save image file from App to Local(no network request)
import RNFetchBlob from 'react-native-fetch-blob';
import RNFS from'react-native-fs';
let dirs = RNFetchBlob.fs.dirs
const file_path = dirs.DCIMDir + '/' + 'wechat.jpg';
RNFS.copyFileAssets('wechat.jpg', file_path)
.then(() => {
RNFetchBlob.fs.scanFile([{ path: file_path }])
})
.catch((error) => console.log(error));
@1c7
Copy link
Author

1c7 commented Jan 17, 2018

wechat.jpg should place under /android/app/src/main/assets/wechat.jpg

@1c7
Copy link
Author

1c7 commented Jan 17, 2018

This code save image from App to local.

I only tested on Android.

@1c7
Copy link
Author

1c7 commented Jan 17, 2018

must RNFetchBlob.fs.scanFile.
otherwise even image exist, the Album app wouldn't show it right the way.

@1c7
Copy link
Author

1c7 commented Jan 17, 2018

I hope it save your time.
I spend 30 minute on this. it's ridiculous

@1c7
Copy link
Author

1c7 commented Jan 17, 2018

if you want test if file exist under /android/app/src/main/assets/

import RNFS from'react-native-fs';

RNFS.existsAssets('wechat.jpg').then((result) => console.log(result));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment