如何使用phonegap在Sdcard中创建文件

Piy*_*ush 5 android cordova

如何使用phonegap在Android Sdcard中创建文件并保存文件?

SHA*_*ANK 7

// 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);
}
Run Code Online (Sandbox Code Playgroud)

这里创建文件编写器方法提供了文件系统的句柄.在success函数中,我们得到名为'file_name.txt'的文件,如果存在则打开,否则创建它.

  • 这不会将文件保存在SD卡中.它将文件保存在手机内存中.如何在SD卡中保存文件? (3认同)