如何在android应用程序空间中存储和检索文件?
我正在开发cordova/phonegap应用程序。现在,我正在Android上测试这个应用程序 如果我默认包含文件(音频、视频等),我可以访问指示“url”的文件,如audio/filesong.mp3或video/filevideo.mp4 但如果我下载带有下一个代码的文件:如何在android应用程序空间中存储和检索文件?,android,cordova,local-storage,Android,Cordova,Local Storage,我正在开发cordova/phonegap应用程序。现在,我正在Android上测试这个应用程序 如果我默认包含文件(音频、视频等),我可以访问指示“url”的文件,如audio/filesong.mp3或video/filevideo.mp4 但如果我下载带有下一个代码的文件: function downloadFile() { var fileTransfer = new FileTransfer(); var uri = encodeURI("UrlOfTheFile");
function downloadFile() {
var fileTransfer = new FileTransfer();
var uri = encodeURI("UrlOfTheFile");
var fileURL = "cdvfile://localhost/persistent/appcustomstorage/";
fileTransfer.download(
uri, fileURL + "file.extension", function(entry) {
console.log("download complete: " + entry.toURL());
}, function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
}, false, {
headers: {
"Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
}
});
}
我在StorageRoot/appcustomstorage/file.extension中下载该文件
可以将文件存储在应用程序包中,例如,存储在Android/data/com.example.app??
还是一种获取Android/data/com.example.app url然后添加必要文件夹的方法
解决方案:
在这种情况下,我在设备中找到所有mp3。它在Nexus4上工作
index.html
<ul data-role="listview" data-inset="true" id="ulsongs">
</ul>
storageScript:
var FileSystem = null;
var AudioExtensions = [ '.mp3' ];// , '.wav', '.m4a' ];
var my_media = null;
function listDir(directoryEntry, level) {
if (level === undefined)
level = 0;
var directoryReader = directoryEntry.createReader();
directoryReader.readEntries(function(entries) { // success get files and
// folders
for ( var i = 0; i < entries.length; ++i) {
if (entries[i].name === '.')
continue;
if (entries[i].isDirectory) {
FileSystem.root.getDirectory(entries[i].fullPath.slice(1,
entries[i].fullPath.length), {
create : false
}, function(dirEntry) {
listDir(dirEntry, level + 1);
}, function(error) {
console.log('ERROR');
alert(error.code);
});
}
if (entries[i].isFile) {
var extension;
extension = entries[i].name.substr(entries[i].name
.lastIndexOf('.'));
if (entries[i].isFile === true
&& $.inArray(extension, AudioExtensions) >= 0) {
// Add a song to the list
$("#ulsongs").append(
"<li id='" + entries[i].fullPath + "'"
+ " data-icon=\"audio\"><a>"
+ entries[i].name + "</a></li>");
$('#ulsongs').listview('refresh');
}
}
}
}, function(error) { // error get files and folders
alert('Error. Code: ' + error.code);
});
// Action listener
$('#ulsongs li').click(function(e) {
pathsong = $(this).attr('id');
console.log('item clicked. Path: ' + pathsong);
if (typeof (pathsong) != 'undefined' && pathsong != null) {
// Stop previous song
if ((audio_status != null) && (audio_status == 2)) {
console.log('STOP AUDIO');
my_media.stop();
my_media.release();
}
// Play the audio file at url
my_media = new Media(pathsong,
// success callback
function() {
console.log("playAudio():Audio Success");
},
// error callback
function(err) {
console.log("playAudio():Audio Error: " + err);
}, status);
// Play audio
my_media.play();
}
});
}
var audio_status = null;
function status(stat) {
audio_status = stat;
}
/**
*
*/
function getFileSystem() {
console.log('entra getFileSystem');
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function(fileSystem) { // success get file system
var sdcard = fileSystem.root;
sdcard.getDirectory('', {
create : false
}, function(dirEntry) {
listDir(dirEntry);
}, function(error) {
alert(error.code);
})
}, function(evt) { // error get file system
console
.log('ERROR GETTING FILE SYSTEM'
+ evt.target.error.code);
});
}
var FileSystem=null;
var AudioExtensions=['.mp3'];/,'。wav','.m4a'];
var my_media=null;
函数listDir(目录项,级别){
如果(级别===未定义)
级别=0;
var directoryReader=directoryEntry.createReader();
readEntries(函数(entries){//success-get-files和
//文件夹
对于(变量i=0;i=0){
//将歌曲添加到列表中
$(“#ulsongs”)。追加(
“”
+条目[i]。名称+“ ”;
$('#ulsongs')。列表视图('refresh');
}
}
}
},函数(错误){//获取文件和文件夹时出错
警报('错误代码:'+错误代码);
});
//动作监听器
$('ulsongs li')。单击(函数(e){
pathsong=$(this.attr('id');
console.log('item clicked.Path:'+pathsong);
if(typeof(pathsong)!=“未定义”&&pathsong!=null){
//停止上一首歌
如果((音频状态!=null)和&(音频状态==2)){
console.log(“停止音频”);
我的媒体。停止();
my_media.release();
}
//在url播放音频文件
my_media=新媒体(pathsong,
//成功回调
函数(){
log(“playAudio():音频成功”);
},
//错误回调
功能(err){
log(“playAudio():音频错误:+err”);
},身份);
//播放音频
我的媒体播放();
}
});
}
var audio_status=null;
功能状态(stat){
音频状态=统计;
}
/**
*
*/
函数getFileSystem(){
log('entra getFileSystem');
requestFileSystem(LocalFileSystem.PERSISTENT,0,
函数(文件系统){//成功获取文件系统
var sdcard=fileSystem.root;
sdcard.getDirectory(“”{
创建:false
},函数(dirEntry){
listDir(dirEntry);
},函数(错误){
警报(错误代码);
})
},函数(evt){//获取文件系统时出错
安慰
.log('获取文件系统时出错'
+evt.target.error.code);
});
}
我创建了一个mp3列表。
我使用jQuery和jQueryMobile
编辑
这个过程可能需要一些时间。建议显示加载弹出窗口
var FileSystem = null;
var AudioExtensions = [ '.mp3' ];// , '.wav', '.m4a' ];
var my_media = null;
function listDir(directoryEntry, level) {
if (level === undefined)
level = 0;
var directoryReader = directoryEntry.createReader();
directoryReader.readEntries(function(entries) { // success get files and
// folders
for ( var i = 0; i < entries.length; ++i) {
if (entries[i].name === '.')
continue;
if (entries[i].isDirectory) {
FileSystem.root.getDirectory(entries[i].fullPath.slice(1,
entries[i].fullPath.length), {
create : false
}, function(dirEntry) {
listDir(dirEntry, level + 1);
}, function(error) {
console.log('ERROR');
alert(error.code);
});
}
if (entries[i].isFile) {
var extension;
extension = entries[i].name.substr(entries[i].name
.lastIndexOf('.'));
if (entries[i].isFile === true
&& $.inArray(extension, AudioExtensions) >= 0) {
// Add a song to the list
$("#ulsongs").append(
"<li id='" + entries[i].fullPath + "'"
+ " data-icon=\"audio\"><a>"
+ entries[i].name + "</a></li>");
$('#ulsongs').listview('refresh');
}
}
}
}, function(error) { // error get files and folders
alert('Error. Code: ' + error.code);
});
// Action listener
$('#ulsongs li').click(function(e) {
pathsong = $(this).attr('id');
console.log('item clicked. Path: ' + pathsong);
if (typeof (pathsong) != 'undefined' && pathsong != null) {
// Stop previous song
if ((audio_status != null) && (audio_status == 2)) {
console.log('STOP AUDIO');
my_media.stop();
my_media.release();
}
// Play the audio file at url
my_media = new Media(pathsong,
// success callback
function() {
console.log("playAudio():Audio Success");
},
// error callback
function(err) {
console.log("playAudio():Audio Error: " + err);
}, status);
// Play audio
my_media.play();
}
});
}
var audio_status = null;
function status(stat) {
audio_status = stat;
}
/**
*
*/
function getFileSystem() {
console.log('entra getFileSystem');
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function(fileSystem) { // success get file system
var sdcard = fileSystem.root;
sdcard.getDirectory('', {
create : false
}, function(dirEntry) {
listDir(dirEntry);
}, function(error) {
alert(error.code);
})
}, function(evt) { // error get file system
console
.log('ERROR GETTING FILE SYSTEM'
+ evt.target.error.code);
});
}