【问题标题】:Read File as Array From SD Card in cordova (ionic) with cordova-plugin-file使用cordova-plugin-file从cordova(离子)中的SD卡读取文件作为数组
【发布时间】:2018-12-06 18:29:24
【问题描述】:

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

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

之后,我将它发送到 Cordova Plugin File resolveNativePath 函数,我得到一个 Path Like:

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 返回“未找到”。

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

{
    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,它会抛出编码错误。 如果我只从fullPath 获得没有名称的“路径”,它也会引发编码错误

有人有类似经历吗?

【问题讨论】:

    标签: cordova ionic-framework ionic2 ionic3 cordova-plugins


    【解决方案1】:

    我最终制作了自己的 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;
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-27
      • 1970-01-01
      • 2019-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多