Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Static Path for an Asset in react native

In my react native app i am showing several images in <Image> tags and i load those images from local folder.

<Image source={require('./src/images/image1.jpg')} />

I want to save image when the user tapped on it. I can get which image user tapped and pass it to the function. But i put a single image path to it.

_onPressButton(imgName) {
  CameraRoll.saveToCameraRoll( './src/images/image1.jpg' , 'photo').then(function(result) {
    alert(result);
  }).catch(function(error) {
    alert(error);
  });
}

But this gives me an error in iOS emulator saying it cant find the image in the path.

When i give as,

CameraRoll.saveToCameraRoll( 'https://i.imgur.com/JnrwMpZ.jpg' , 'photo')

It works.

But i want to save these files in my src/images folder. How can i get a path of this image file OR get this done..?

I appreciate any help regarding this.

Thanks

like image 704
Janith Ganewatta Avatar asked May 18 '26 03:05

Janith Ganewatta


1 Answers

Here is my solution with Expo, worked in android and should work on ios too

onSave = async () => {
  const asset = Asset.fromModule(require('./src/images/image1.jpg'))
  if (!asset.localUri) {
    await asset.downloadAsync();
  }
  const uri = asset.localUri;
  CameraRoll.saveToCameraRoll(uri, 'photo')
}
like image 83
izht Avatar answered May 20 '26 02:05

izht



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!