And*_*ndi 10 file-io android cordova
问题描述:
我可以使用文件或文件系统根(读取和写入)访问内部存储.但是这样的文件无法从其他应用程序访问.例如,如果我想通过emailComposerPlugin发送此文件,则电子邮件客户端无法访问该文件.(同样适用于"打开方式"功能.)如果我将选项更改{sandboxed: true}为false(写入外部存储器),则它不起作用并最终在FileUtils.UNKNOWN_ERR中.当手机与USB断开连接时,我尝试了应用程序,因为有些文档提到在安装到PC上时无法访问外部存储 - 但结果相同.
从我在邮件列表上看到的内容来看,这应该是可能的.看来我错过了一个关键点?
上下文: 我尝试启用为iPhone创建的混合应用程序以在Android设备上运行.为了有一个小游乐场,我创建了一个小型测试项目.
编辑: 文件系统根目录和文件插件之间似乎存在问题.但我有他们两个的最新版本.(文件:1.0.1文件系统根:0.1.0)调试文件系统和文件类显示
private String fullPathForLocalURL(Uri URL) {
if (FILESYSTEM_PROTOCOL.equals(URL.getScheme()) && "localhost".equals(URL.getHost())) {
String path = URL.getPath();
if (URL.getQuery() != null) {
path = path + "?" + URL.getQuery();
}
return path.substring(path.indexOf('/', 1));
// path = "/cache-external" at this point
// results in index out of bounds exception
Run Code Online (Sandbox Code Playgroud)
我试过了什么?
config.xml中
<preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external" />
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml中
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)
javascript代码
function createTextDocument(filename, text) {
cordova.filesystem.getDirectoryForPurpose('cache', {sandboxed: false}, successCallback, failureCallback);
function successCallback(directoryEntry){
console.log('directory found (cordova): ' + directoryEntry.toURL());
console.log('directory found (native) : ' + directoryEntry.toNativeURL());
directoryEntry.getFile(filename, {create: true, exclusive: false},
function(fileEntry){
var filePath = fileEntry.toNativeURL();
fileEntry.createWriter(
function(fileWriter){
console.log('start writing to: ' + filePath );
fileWriter.write(text);
console.log('file written');
},failureCallback
);
}, failureCallback
);
}
function failureCallback(error){
console.log('error creating file: ' + error.code);
// results in code 1000
}
}
Run Code Online (Sandbox Code Playgroud)
And*_*ndi 13
在仔细研究了整个主题后,我发现:
JavaScript用法:
window.resolveLocalFileSystemURL(path, cbSuccess, cbFail);
@param path: {string} a cordova path with scheme:
'cdvfile://localhost/<file-system-name>/<path-to-file>'
Examples: 'cdvfile://localhost/sdcard/path/to/global/file'
'cdvfile://localhost/cache/onlyVisibleToTheApp.txt'
@param cbSuccess: {function} a callback method that receives a DirectoryEntry object.
@param cbFail: {function} a callback method that receives a FileError object.
Run Code Online (Sandbox Code Playgroud)
config.xml中
<preference name="AndroidPersistentFileLocation" value="Compatibility" />
<preference name="AndroidExtraFilesystems" value="sdcard,cache" />
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml中
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)
经过几个小时的挣扎,最终我能够通过提供像"files:// pathtofile /"这样的目录来扫描SD卡并获取对文件的访问权限.
我已经提交了一个API和一个示例项目
https://github.com/xjxxjx1017/cordova-phonegap-android-sdcard-full-external-storage-access-library
使用API的示例
new ExternalStorageSdcardAccess( fileHandler ).scanPath( "file:///storage/sdcard1/music" );
function fileHandler( fileEntry ) {
console.log( fileEntry.name + " | " + fileEntry.toURL() );
}
Run Code Online (Sandbox Code Playgroud)
API源代码
var ExternalStorageSdcardAccess = function ( _fileHandler, _errorHandler ) {
var errorHandler = _errorHandler || _defultErrorHandler;
var fileHandler = _fileHandler || _defultFileHandler;
var root = "file:///";
return {
scanRoot:scanRoot,
scanPath:scanPath
};
function scanPath( path ) {
window.resolveLocalFileSystemURL(path, _gotFiles, errorHandler );
}
function scanRoot() {
scanPath( root );
}
function _gotFiles(entry) {
// ? Check whether the entry is a file or a directory
if (entry.isFile) {
// * Handle file
fileHandler( entry );
}
else {
// * Scan directory and add media
var dirReader = entry.createReader();
dirReader.readEntries( function(entryList) {
entryList.forEach( function ( entr ) {
_gotFiles( entr );
} );
}, errorHandler );
}
}
function _defultFileHandler(fileEntry){
console.log( "FileEntry: " + fileEntry.name + " | " + fileEntry.fullPath );
}
function _defultErrorHandler(error){
console.log( 'File System Error: ' + error.code );
}
};
Run Code Online (Sandbox Code Playgroud)
配置
config.xml
删除首选项:首选项名称="AndroidExtraFilesystems"
确保测试环境可以在测试时访问自己的外部存储.(如果你用usb连接它,请确保它作为一个摄像头连接;在收到"deviceready"事件后调用API)
路径示例:
file:///
file:/// somefile/
file:/// somefile
file:///somefile/music/aaaaa.mp3
| 归档时间: |
|
| 查看次数: |
29838 次 |
| 最近记录: |