react-nativeexpoexpo-file-system

Error When Downloading Image Using Expo File System React Native IOS


I want to download the image file from web. This is my code :

import React from 'react'
import * as Sharing from "expo-sharing";
import * as FileSystem from 'expo-file-system';
import * as MediaLibrary from 'expo-media-library';
import { getCurrentDateTime } from './UtilityHelper';
import { messageBoxOK } from './AlertHelper';

export const downloadFileIOS = async (url, ticker) => {        
    let filename = ticker + "_" + getCurrentDateTime() + ".png";     

    MediaLibrary.requestPermissionsAsync();       
    if (filename !== null) {
        FileSystem.downloadAsync(
            url,
            FileSystem.documentDirectory + filename
        ).then(async ({uri}) => {
            console.log('Finished downloading to ', uri);            
            MediaLibrary.createAssetAsync(uri).then(asset => {
                console.log('asset', asset);                
                MediaLibrary.createAlbumAsync('My Gallery', asset, false)
                    .then(() => {                        
                        messageBoxOK("Download", "Success");
                    })
                    .catch(error => {
                        messageBoxOK("Download", "Failed");
                    });
                });                
            }).catch(error => {
                console.error(error);
            });    
    }
};

It worked well on Android but not for IOS.

For IOS I got this error :

[Error: Directory for 'file:///Users/dennisliu/Library/Developer/CoreSimulator/Devices/632FD441-0040-4E1A-AA4E-52A5C015C304/data/Containers/Data/Application/EBFB20FF-EAD8-40FE-BE4D-1D1719A633D2/Documents/ASII_2022-09-07 07:37:36.png' doesn't exist. Please make sure directory '(null)' exists before calling downloadAsync.]

I have allow the permission in IOS.

What could be the problem ?


Solution

  • I found the problem.

    The problem is the filename : ASII_2022-09-07 07:37:36.png is not valid and causing the error.

    When I change the filename to : AAA.png it worked.