[Solved] How to copy an image from assets to cache directory or maybe there is another way...


I am using (and this works fine) an ImagePicker component to make a photo with a camera, and then send it to a server.
I send it with

formData.append('pic', { uri: imagePickerResult.uri });

where imagePickerResult looks e.g. like this:

Object {
            "cancelled": false, "type": "image",
            "height": 960,"width": 1280,
            "uri": "file:///data/user/0/host.exp.exponent/cache/ExperienceData/ImagePicker/5df50fe9.jpg",

And yes, it works. But… for the purposes of testing, I’m a bit tired of really making the photos. It’s only a part of the whole process which needs to be done to complete the tests.

So I thought that I will just simulate it. I added a few random photos into assets/images/simulating

And… now what? I tried to find something, but failed. I guess I should probably use FileSystem to copy the asset file to somewhere else (like FileSystem.cacheDirectory), right? Or maybe… I can use a path to the image in the assets directory directly?

But… the Asset.fromModule(require('../assets/images/simulating/image-1.jpg')).uri; returns a … remove url. I just don’t really get these things :frowning:

I just want to fake the above imagePickerResult, but with real file, so the upload can actually work.

SDK : 34.0.4 , platforms: all

OK, I did it! by actually randomly putting characters here and there :slight_smile:

const asset = Asset.fromModule(require('../assets/images/simulating/image-1.jpg'));
        if (!asset.localUri) {
            await asset.downloadAsync();
        const uri = asset.localUri;

Edit: Hmmm, I can’t see how I can mark this as ‘answered’ though.

1 Like

You could edit the subject to say “[Solved] How to copy…”. I don’t know if there’s a better way.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.