dsmatana
dsmatana

Reputation: 61

FileReader FileError error code 1 NOT_FOUND_ERR (misleading) with Cordova plugins cordova-plugin-file and cordova-plugin-media-capture on Android

I'm struggling to create voice recorder for messaging app on android 10 using cordova. Simply put, I can't read file recorded by pre-instaled voice recorder.

After invoking navigator.device.capture.captureAudio and permitting storage permission, voice recorder pops up, records audio and after submitting, it returns MediaFile saved in recorders's folder.

If i'm trying to read the file, but somehow it pops FileError with code 1 which means NOT_FOUND_ERR - that's not true, file exists and I have storage permissions. I've been dealing with this in the past, this chunk of code was working, but after some time and numerous resetting android platform it's not working again.

What prevents me to read file from another application ? (I'm new to native mobile apps).

Thanks a lot.

versions used: [email protected], [email protected], [email protected], [email protected]

const INTERNAL_ERR = {
    code: 0,
    message: 'The microphone failed to capture sound.',
};

return new Promise((resolve, reject) => {
    navigator.device.capture.captureAudio(
        ([mediaFile]) => {
            console.log(mediaFile); // *1
            window.resolveLocalFileSystemURL(
                mediaFile.localURL, // using cdvfile url
                fileEntry => {
                    console.log(fileEntry); // *2
                    fileEntry.file(
                        file => {
                            const reader = new FileReader();
                            reader.onloadend = e => {
                                if (e.target.result) {
                                    resolve(e.target.result);
                                } else {
                                    reject(INTERNAL_ERR);
                                }
                            };
                            reader.onerror = e => console.error(e); // *3
                            reader.readAsDataURL(file);
                        },
                        error => reject(INTERNAL_ERR)
                    );
                },
                error => reject(INTERNAL_ERR)
            );
        },
        error => reject(error),
        { limit: 1, duration: 60, }
    );
}

Created MediaFile from cordova-plugin-media-capture - console.log *1

MediaFile {name: "Voice 020.m4a", localURL: "cdvfile://localhost/persistent/Voice%20Recorder/Voice%20020.m4a", type: "audio/mpeg", lastModified: null, lastModifiedDate: 1612256449000, …}
    end: 0
    fullPath: "file:///storage/emulated/0/Voice%20Recorder/Voice%20020.m4a"
    lastModified: null
    lastModifiedDate: 1612256449000
    localURL: "cdvfile://localhost/persistent/Voice%20Recorder/Voice%20020.m4a"
    name: "Voice 020.m4a"
    size: 90812
    start: 0
    type: "audio/mpeg"

Created FileEntry - console.log *2

FileEntry {isFile: true, isDirectory: false, name: "Voice 020.m4a", fullPath: "/Voice Recorder/Voice 020.m4a", filesystem: FileSystem, …}
    filesystem: FileSystem
    name: "persistent"
    root: DirectoryEntry {isFile: false, isDirectory: true, name: "", fullPath: "/", filesystem: FileSystem, …}
    fullPath: "/Voice Recorder/Voice 020.m4a"
    isDirectory: false
    isFile: true
    name: "Voice 020.m4a"
    nativeURL: "file:///storage/emulated/0/Voice%20Recorder/Voice%20020.m4a"

Popped event - console.error *3

ProgressEvent {type: "error", bubbles: false, cancelBubble: false, cancelable: false, lengthComputable: false, …}
target: FileReader
    _error: FileError
    code: 1

Upvotes: 1

Views: 1807

Answers (0)

Related Questions