I'm making a function to save a file downloaded from a server via expo-file-system. In this code, when you click the download button on Android, the file selection UI appears, and when you select a file, the file is downloaded. However, I want files to be automatically downloaded in a specific directory without user selection, like a normal application.

When using requestDirectoryPermissionAsync, is it possible to directly download a file to a specific path without SelectionUI? If StorageAccessFramework doesn't have such a way, is there a way to solve this problem in expo-file-system?

code

import * as FileSystem from "expo-file-system";

const { StorageAccessFramework } = FileSystem;

const saveAndroidFile = async (fileUri, fileName, contentType) => {
    try {
        const fileDir = StorageAccessFramework.getUriForDirectoryInRoot("Download");

        // This function calls the UI in the image below
        const permissions = await StorageAccessFramework.requestDirectoryPermissionsAsync(fileDir);
        if (!permissions.granted) {
            return;
        }

        try {
            await StorageAccessFramework.createFileAsync(fileDir, fileName, contentType)
                .then(async (uri) => {
                    await FileSystem.writeAsStringAsync(uri, fileString, { encoding: FileSystem.EncodingType.Base64 });
                    alert("Report Downloaded Successfully");
                })
                .catch((e) => {
                    console.log(e);
                });
        } catch (e) {
            throw new Error(e);
        }
    } catch (err) {}
};

Folder SelectionUI, which opens whenever requestDirectoryPermissionAsync is called enter image description here

0

There are 0 best solutions below