How to use Expo's FileSystem properly

#1

Hi,

I’m looking into ways to preserve an avatar picture taken with ImagePicker.
using AsyncStorage seems to be a no go since there’s size limitation.
So I’m trying to figure out how to use FileSystem.

What I understand is that:

  1. After I get the base64 encoded image from ImagePicker, I need to make a directory to store it by using Expo.FileSystem.makeDirectoryAsync(Expo.FileSystem.cacheDirectory+'/avatar')
  2. Then I have to save the image using Expo.FileSystem.writeAsStringAsync(Expo.FileSystem.cacheDirectory+'/avatar', 'data:image/jpg;base64,'+base64Image)
  3. Retrieve it by using Expo.FileSystem.readAsStringAsync(Expo.FileSystem.cacheDirectory+'/avatar')

My question is:

  1. The doc said that cacheDirectory is for one use only since it may be deleted any time on system’s discretion, does that mean in my case if I want to save this avatar permanently I have to use fileDirectory instead in the above scenario?
  2. When the app is deleted, will the saved file deleted with the app or is that something I have to explicitly handle? (in cases of cacheDirectory and fileDirectory)
  3. When user renews the avatar by taking new picture and makeDirectoryAsync is called again with the same exact command as written above for the second time and on, would it give me error saying the directory already exist or something like that?
  4. When user take new picture and save in the /avatar directory, would it be saved as /avatar/image2 or will it overwrite /avatar/image1?

Sorry for the basic questions, I just can’t seem to find any clue as to what happens in the background just based on the documentations…
And I know it’s weird, saving avatar to phone instead of to server then retrieving it from there but it’s just a hack-style decision by my project manager so yeah I have to find out how to do this properly. lol

Thanks in advance! :smiley:

UPDATE:
based on #1245, readAsStringAsync() could not be used for jpeg, is this still the case now? and if it is, is there any way I can make my use case work?

#2

I’ve recently used this, but I’m not an expert. From my experience if you want the item to be there for a long duration (ie days) then you shouldn’t put it in the cacheDirectory. I’m not sure about whether documents in fileDirectory persist after removing the app from a phone though :frowning:

#3

Thanks for the reply, and u are right I have checked on this few days ago and confirmed cacheDiretory behaviour, still abit blurry about fileDirectory though, I guess we’ll find out when the app’s users report it lol

closed #4

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