[英]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.