0

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 ?

2
  • check this stackoverflow.com/a/67272960/15336413 or you can change the library if it has some problems Commented Sep 7, 2022 at 8:10
  • @chikabala Thanks, but I found that this filename : ASII_2022-09-07 07:37:36.png is not valid and causing the errror. When I change the filename to AAA.png it worked. Commented Sep 7, 2022 at 8:17

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

yeah not a valid one because it contains spaces

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.