【问题标题】:Phonegap cordova create directory on micro SDPhonegap cordova 在 micro SD 上创建目录
【发布时间】:2018-02-15 16:23:55
【问题描述】:

我正在使用 phonegap/cordova 在 android 上开发一个应用程序。

我正在尝试使用以下代码在我的 micro SD 上创建一个目录:

function createMicroSdDir(){
window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory,
function fileEntryCallback(fileEntry) {
fileEntry.getDirectory("myDirectory", { create: true, exclusive: false });
}
);
}

但目录是在内部存储器上创建的。

有什么想法吗?提前谢谢

【问题讨论】:

  • externalRootDirectory 是模拟存储,也就是内部存储器。即使使用原生 Android 也很难操作 micro SD 文件。为此,您必须使用 Storage Access Framework。但它适用于原生 Android。不幸的是,我不知道如何用cordova做到这一点。

标签: android cordova phonegap


【解决方案1】:

写入文件示例:

var fileName = 'myfile.txt';
var data = '...';
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 ); } );

【讨论】:

  • Tks,但文件再次在内部存储器上创建。
  • @npm 确保您的 Manifest.xml 具有写入外部存储权限。
  • 请在此处阅读所有帖子:storage
【解决方案2】:

cordova-plugin-file 不支持在 Android 上引用可移动 micro-SD 卡位置。仅支持内部模拟的“SD 卡”位置。

但是,您可以使用cordova-diagnostic-plugin 中的getExternalSdCardDetails() 函数来获取可移动外部SD 卡的位置和详细信息。

更多详情请见this answer

【讨论】:

  • 函数列表SD(){ alert("NPM1"); cordova.plugins.diagnostic.getExternalSdCardDetails(function(details){ alert("NPM2"); details.forEach(function(detail){ alert("detail.freeSpace = [" + detail.freeSpace + "]"); alert( "detail.filePath = [" + detail.filePath + "]"); if(detail.canWrite && details.freeSpace > 100000){ cordova.file.externalSdCardDirectory = detail.filePath; // 然后:将文件写入外部 SD 卡} }); }, 函数(错误){ 警报(错误); }); }
  • 能否请您添加一个从诊断插件获取 sd 卡路径后如何创建目录的示例?
猜你喜欢
  • 2013-09-26
  • 2011-01-09
  • 2018-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多