gpt4 book ai didi

cordova - ionic cordova 从 sdcard 获取所有 mp3 文件

转载 作者:行者123 更新时间:2023-12-01 13:16:33 25 4
gpt4 key购买 nike

我正在开发一个音乐应用程序,我需要从 Android 设备中检索所有 mp3 文件。我怎样才能用 ionic 和 cordova 做到这一点?

我曾尝试使用 cordova 文件插件,但我什至找不到一种方法来列出目录中的所有文件。请帮忙。

 window.resolveLocalFileSystemURI(cordova.file.externalRootDirectory, function(fileSystem) {
var directoryReader = fileSystem.createReader();
directoryReader.readEntries(function(entries) {
//alert(JSON.stringify(directoryReader));
for(i=0;i<entries.length;i++)
{
alert(JSON.stringify(entries[i].name));

}
//alert(entries);
//console.log(entries);
});
});

上面的代码只是给出了sd卡中的目录名。我需要一个逻辑来从所有目录和子目录中查找 mp3 文件。

最佳答案

如果有人想知道如何在 ionic2/3 上执行此操作,我们可以使用 cordova-plugin-file 从系统中检索所有文件。

首先安装 cordova-file-plugin。

将此插件添加到您应用的模块中。

然后像这样在任何地方使用它。

import { FilePath } from '@ionic-native/file-path';
import { File } from '@ionic-native/file';
constructor(....
, public filePath: FilePath
, public platform: Platform
, public file: File ) {
this.platform.ready().then(() => {
//the first parameter file.externalRootDirectory is for listing all files on application's root directory
//The second parameter is the name of the folder. You can specify the nested folder here. e.g. 'Music/Coldplay'
file.listDir(file.externalRootDirectory, '').then((result) => {
for(let item of result)
{
if(item.isDirectory == true && item.name != '.' && item.name!= '..')
{
this.getFileList(item.name);//Get all the files inside the folder. recursion will probably be useful here.
}
else if (item.isFile == true)
{
//File found
this._fileList.push({
name: item.name,
path: item.fullPath
});
}
}
},
(error) => {
console.log(error);
});
})
}

public getFileList(path: string): any
{
let file = new File();
this.file.listDir(file.externalRootDirectory, path)
.then((result)=>{
for(let item of result)
{
if(item.isDirectory == true && item.name != '.' && item.name != '..')
{
this.getFileList(path + '/' + item.name);
}
else
{
this._fileList.push({
name: item.name,
path: item.fullPath
})
}
}
},(error)=>{
console.log(error);
})
}

**您可以通过比较提取过程中某处的扩展类型来过滤音频文件

Complete cordova documentation

Ionic documentation

关于cordova - ionic cordova 从 sdcard 获取所有 mp3 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34384319/

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