How do i store images/files

#1

Hi there,

I’ve been using React Native Fetch Blob for downloading images and storing them to be used throughout my app, i’ve since moved to and tried to use it on Expo but i get errors like RNFetchBlob.DocumentDir is undefined and etc.

Then i tried using React Native FS which also produces the same sort of error

Which library or how do i store images/files with expo?

1 Like
#2

I’d recommend taking a look at the Asset docs, specifically Asset.downloadAsync. Let us know if that doesn’t work for you?

#3

alright i’ll try that out, thanks

#4

Are there examples of how to use it?
No idea where to put the URL

import Expo from ‘expo’

Expo.Asset.downloadAsync(url)?

#5

This was how i was doing it before

let imageLocation = roomImageDir+'roomImg-'+roomId;

	RNFetchBlob.fs.isDir(roomImageDir)
	.then((isDir) => {
		if(isDir === false) {
			RNFetchBlob.fs.mkdir(roomImageDir)
			.catch((err) => {
				// do nothing
				// console.log(err)
			})
		}
	})
	

	return (dispatch) => {
		dispatch(requestRoomImage(roomId))

		RNFetchBlob.fs.exists(imageLocation)
		.then((exists) => {
			if (exists)
			{
				pImage = 'file://' + imageLocation
				dispatch(receiveRoomImage(roomId,pImage))
			}
			else
			{
				return RNFetchBlob.config({
						appendExt: 'jpg',
					})
				.fetch('GET', 'https://theapiurl/getimage/'+roomId)
				.then((res) => { 

					let pImage = ''
					if(res.data) {
						RNFetchBlob.fs.writeFile(imageLocation, res.data, 'base64')	
						pImage = 'file://' + imageLocation
					}
					dispatch(receiveRoomImage(roomId,pImage))
				})
			}
		})
		
	}
#6

Well just to reply myself, i just did a Image.prefetch instead thinking i dont really need to specify where i save the image

let imageURL = 'https://mysite.com/getroomimg/'+roomId
return (dispatch) => {
        dispatch(requestRoomImage(roomId))
        Image.prefetch(imageURL)
        .then( () => {
	       dispatch(receiveRoomImage(roomId,imageURL))
        })
}
#7

I have same question regarding downloading but for video… i have videos each with size of more than 5MB so before play i want to download those videos to local storage so definitly i want to show some progress bar to users. as we can;t integrate RNFS or RNFETCHBLOB with expo… question is can we get progress of downloading with expo assets way?

#8

Try this one!! It works with expo@v25 and I’m using it in my project.