使用cordova-plugin-file从cordova(离子)中的SD卡读取文件作为数组 [英] Read File as Array From SD Card in cordova (ionic) with cordova-plugin-file

查看:24
本文介绍了使用cordova-plugin-file从cordova(离子)中的SD卡读取文件作为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use audio files in my app that I need to get as an buffer array. For this I let the user choose a file (using Ionic/Cordova FileChooser Plugin) and then I get an URL like:

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

之后,我将其发送到 Cordova 插件文件 resolveNativePath 函数,我得到一个 Path Like:

After that, I sent this to Cordova Plugin File resolveNativePath function and I get a Path Like:

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

在这里我创建了我的 audioFileInfo 对象

Here I make my audioFileInfo Object

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) 来获取缓冲区数组.

and finally I call filePlugin.readAsArrayBuffer(audioFileInfo.path, audioFileInfo.name) to get the buffer array.

当文件在设备内部存储时它可以正常工作,但是当文件来自 SDCard 时它不起作用,因为 readAsArrayBuffer 返回未找到".

It works ok when the file is in the device internal storage, but when the file comes from the SDCard it does not work because the readAsArrayBuffer returns "not found".

SD 卡:

文件选择器网址

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 对象:

I have tried FilePlugins's resolveLocalFilesystemUrl() and I get this Entry object:

{
    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.

Bue I have no idea what to use as path in the first parameter of the readAsArrayBuffer function.

如果我使用 fullPathname 它会抛出 encoding error.如果我只从 fullPath 获得没有名称的路径",它也会抛出 encoding error.

If I use fullPath and name it throws encoding error. If I get just the "path" without the name from fullPath, it also throws encoding error.

有人有类似经历吗?

推荐答案

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

I ended up making my own FileReader to process the file with the info returned by resolveLocalFilesystemUrl, Please be aware that this code uses 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;

这篇关于使用cordova-plugin-file从cordova(离子)中的SD卡读取文件作为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆