繁体   English   中英

Phonegap Android写入SD卡

[英]Phonegap Android write to sd card

如果我在手机上使用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调试。 它没有显示任何错误。 我认为该文件是在其他地方创建的。 但是我需要在sdcard的根目录或内部存储器上写入文件。

请帮忙!

谢谢,

写入文件示例:

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 ); } );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM