给出下面的代码,如果我在移动设备上使用 Phonegap Developer 进行测试,它会在我的 Nexus 5 设备的根目录中创建一个文件。
// create a file writer object
function CreateFileWriter()
{
// request the file system object
window.requestFileSystem( LocalFileSystem.PERSISTENT, 0, OnFileSystemSuccess,fail);
}
function OnFileSystemSuccess( pFileSystemObj )
{
console.log( pFileSystemObj.name );
console.log( pFileSystemObj.root.name );
pFileSystemObj.root.getFile( "file_name.txt", {create: true, exclusive: false}, OnFileGetSuccess, fail);
}
function OnFileGetSuccess( pFileEntryObj )
{
pFileEntryObj.createWriter( function(pWriterObj){
gWriterObj = pWriterObj;
}, fail );
}
function fail(evt)
{
console.log(evt.target.error.code);
}
但是,如果我部署应用程序并生成 apk。在我的设备中安装后。该文件不是在根目录中创建的。
我已在 AndroidManifest.xml 中授予权限并添加了文件插件。另外,从 gapdebug 调试。它没有显示任何错误。我假设该文件是在其他地方创建的。但我需要在 SD 卡或内存的根目录上写入文件。
请帮忙!
谢谢,
最佳答案
写入文件示例:
var fileName = 'myfile.txt'; // your file name
var data = '...'; // your data, could be useful JSON.stringify to convert an object to JSON string
window.resolveLocalFileSystemURL( cordova.file.externalRootDirectory, function( directoryEntry ) {
directoryEntry.getFile(fileName, { create: true }, function( fileEntry ) {
fileEntry.createWriter( function( fileWriter ) {
fileWriter.onwriteend = function( result ) {
console.log( 'done.' );
};
fileWriter.onerror = function( error ) {
console.log( error );
};
fileWriter.write( data );
}, function( error ) { console.log( error ); } );
}, function( error ) { console.log( error ); } );
}, function( error ) { console.log( error ); } );
关于javascript - Phonegap Android 写入 SD 卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34302127/