cordova读取文件_使用cordova-plugin-file在cordova(离子)中从SD卡读取文件作为阵列...

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

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

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"}

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

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 Card:

File Chooser 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"}

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",

}

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

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.

Does anybody had a similar experience?

解决方案

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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值