使用 cordova-plugin-file 从 cordova (ionic) 中的 SD 卡读取文件作为数组

Read File as Array From SD Card in cordova (ionic) with cordova-plugin-file

我在我的应用程序中使用需要作为缓冲区数组获取的音频文件。为此,我让用户选择一个文件 (using Ionic/Cordova FileChooser Plugin),然后我得到一个 URL,例如:

content://com.android.providers.media.documents/document/audio%3A8431

在那之后,我将它发送到 Cordova Plugin File resolveNativePath 函数,我得到了一个像这样的路径:

file:///storage/emulated/0/Prueba interno/Interno, Teddybär, Dreh Dich Um__320kbps.mp3

这里我制作我的 audioFileInfo 对象

audioFileInfo = {name: "Interno, Teddybär, Dreh Dich Um__320kbps.mp3",
                 originalPath: "content://com.android.providers.media.documents/document/audio%3A8431",
                  path: "file:///storage/emulated/0/Prueba interno"}

最后我调用 filePlugin.readAsArrayBuffer(audioFileInfo.path, audioFileInfo.name) 来获取缓冲区数组。

当文件在设备内部存储时它工作正常,但是当文件来自SDCard时它不工作因为readAsArrayBuffer returns "not found".

SD卡:

文件选择器URL

content://com.android.externalstorage.documents/document/3D91-1C14%3AM%C3%BAsica%20Dana%2F1%20-%20Teddyb%C3%A4r%2C%20Teddyb%C3%A4r%2C%20Dreh%20Dich%20Um__320kbps.mp3

resolveNativePath:

file:///sdcard/Música Dana/1 - Teddybär, Teddybär, Dreh Dich Um__320kbps.mp3

audioFileInfo:

audioFileInfo = {
    name :"1 - Teddybär, Teddybär, Dreh Dich Um__320kbps.mp3"
    originalPath : "content://com.android.externalstorage.documents/document/3D91-1C14%3AM%C3%BAsica%20Dana%2F1%20-%20Teddyb%C3%A4r%2C%20Teddyb%C3%A4r%2C%20Dreh%20Dich%20Um__320kbps.mp3",
    path : "file:///sdcard/Música Dana"
}

readAsArrayBuffer:

FileError {code: 1, message: "NOT_FOUND_ERR"}

我试过 FilePlugins 的 resolveLocalFilesystemUrl(),我得到了这个 Entry 对象:

{
    filesystem: {
        name: "content",
        root: {
            filesystem {
                name: "content",
                root: "...."
            }
        },

        fullPath: "/",
        isDirectory: true,
        isFile: false,
        name: "",
        nativeURL: "content://",
    },
    fullPath: "/com.android.externalstorage.documents/document/3D91-1C14:Música Dana/1 - Teddybär, Teddybär, Dreh Dich Um__320kbps.mp3",
    isDirectory: false,
    isFile: true,
    name: "1 - Teddybär, Teddybär, Dreh Dich Um__320kbps.mp3",
    nativeURL: "content://com.android.externalstorage.documents/document/3D91-1C14%3AM%C3%BAsica%20Dana%2F1%20-%20Teddyb%C3%A4r%2C%20Teddyb%C3%A4r%2C%20Dreh%20Dich%20Um__320kbps.mp3",
}

但我不知道在 readAsArrayBuffer 函数的第一个参数中使用什么作为 path

如果我使用 fullPathname,它会抛出 编码错误。 如果我只得到 "path" 而没有来自 fullPath 的名称,它也会抛出 编码错误 .

有没有人有过类似的经历?

我最终制作了自己的 FileReader 以使用 resolveLocalFilesystemUrl 返回的信息处理文件,请注意此代码使用 Typescript


    let entry = await this.filePlugin.resolveLocalFilesystemUrl(audioFileInfo.originalPath);
    let fileEntry: FileEntry = entry as FileEntry;
   
    console.log("fileEntry", fileEntry);
   
    let readFilePromise = new Promise < any > (resolve => {
        console.log("getting File from fileEntry");
        // fileEntry.getMetadata(data => {
        //     console.error("metadata", data); 
        // });
        fileEntry.file((file) => {
            console.log("File", file);
            var reader = new FileReader();
   
            reader.onloadend = () => {
                console.log("Successful file read: ", reader.result);
                resolve(reader.result);
            };
   
            reader.onprogress = (progressEvent) => {
   
                let fileUpdateInfo: FileLoadingUpdateInfo = {
                    audioFileInfo: audioFileInfo,
                    total: progressEvent.total,
                    loaded: progressEvent.loaded,
                    percent: parseFloat((progressEvent.loaded * 100 / progressEvent.total).toFixed(2))
                }
                this.updateAudioFileLoadingText$.next(fileUpdateInfo);
            };
            reader.onerror = (e) => {
                console.error("The reader had a problem", e)
                resolve(undefined);
            }
            reader.onabort = (e) => {
                console.error("We aborted...why???", e)
                resolve(undefined);
            }
            console.log("using reader to readAsArrayBuffer ");
            reader.readAsArrayBuffer(file);
   
   
        }, (e) => {
            console.error(e)
        });
    });
    let fileBufferArray = await readFilePromise;
    console.log("fileBufferArray", fileBufferArray);
   
    return fileBufferArray;