gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 06:32:50 25 4
gpt4 key购买 nike

我在我的应用程序中使用需要作为缓冲区数组获取的音频文件。为此,我让用户选择一个文件 ( 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 返回“未找到”。

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 = {
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 中获取没有名称的“路径”,它也会抛出编码错误

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

最佳答案

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

关于cordova - 使用 cordova-plugin-file 从 cordova (ionic) 中的 SD 卡读取文件作为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51069913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com