简体   繁体   English

如何使用 Cordova 文件插件读取/写入 Android/iOs 设备子目录中的文件?

[英]How can I read/write files in Android/iOs devices subdirectories using Cordova File Plugin?

I'm trying this code:我正在尝试这个代码:

function onDeviceReady() {
requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {   
    alert(fileSystem.root.toURL());

    fileSystem.root.getDirectory('myDir', { create: true }, function (myDirEntry) {
        alert(myDirEntry.toURL());

        myDirEntry.getDirectory('mySubDir', { create: true }, function (mySubDirEntry) {
            alert(mySubDirEntry.toURL());
            alert('/myDir/mySubDir creation is done');

        }, getDirectoryError);
    }, getDirectoryError);      
}, requestFileSystemError);

As a result of executing it on a Samsung SM-G318ML, Kernel version 3.10.17-983426, I got alerts showing the following messages:由于在三星 SM-G318ML 内核版本 3.10.17-983426 上执行它,我收到了显示以下消息的警报:

file:data/data/com.mydomain.myapp/files/files/    
file:data/data/com.mydomain.myapp/files/files/myDir
file:data/data/com.mydomain.myapp/files/files/myDir/mySubDir
/myDir/mySubDir creation is done.

Neverthless, when I look in my device's file system I have no desired directories created... besides it a strange directory, called .mtp, was created, void, at system root.尽管如此,当我查看设备的文件系统时,我没有创建所需的目录......除此之外,在系统根目录下创建了一个名为 .mtp 的奇怪目录,该目录无效。

My goal is create and read files to/from /com.mydomain.mayapp/files/profile/images directory.我的目标是在 /com.mydomain.mayapp/files/profile/images 目录中创建和读取文件。 My code is based on a sample provided (in the end of the page) in Sample Link Followed ;我的代码基于Sample Link Followed 中提供的示例(在页面末尾); I am using Cordova.File plugin, version 4.1.0, in an Intel XDK hybrid app project.我在英特尔 XDK 混合应用项目中使用 Cordova.File 插件,版本 4.1.0。
I have the necessary android.permission.WRITE_EXTERNAL_STORAGE permission defined.我定义了必要的android.permission.WRITE_EXTERNAL_STORAGE权限。

Can someone point me in the right direction?有人可以指出我正确的方向吗? All advices are welcome... thanks in advance.欢迎所有建议......提前致谢。

Another document that I found very useful is here: cordova-plugin-file我发现另一个非常有用的文件在这里: cordova-plugin-file

according to this document:根据这份文件:

cordova.file.dataDirectory - Persistent and private data storage within the application's sandbox using internal memory ( on Android, if you need to use external memory, use .externalDataDirectory ). cordova.file.dataDirectory - 使用内部存储器(在 Android 上,如果您需要使用外部存储器,请使用 .externalDataDirectory )在应用程序沙箱中的持久和私有数据存储。 On iOS, this directory is not synced with iCloud (use .syncedDataDirectory).在 iOS 上,此目录不与 iCloud 同步(使用 .syncedDataDirectory)。 (iOS, Android, BlackBerry 10, windows) (iOS、Android、黑莓 10、Windows)

The directories that you are writting, are inside data/data folders, which belongs only to your app, and can't be accessed thought the device file system.您正在写入的目录位于 data/data 文件夹内,仅属于您的应用程序,无法通过设备文件系统访问。

If you want to use public folders, check this table如果要使用公用文件夹,请查看此表

https://github.com/apache/cordova-plugin-file#android-file-system-layout https://github.com/apache/cordova-plugin-file#android-file-system-layout

And choose a folder that don't be private.并选择一个非私人文件夹。

For example: externalApplicationStorageDirectory例如:externalApplicationStorageDirectory

Happy coding!快乐编码!

暂无
暂无

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

相关问题 我如何编写一个android cordova插件,如果蓝牙打开,它将返回1,否则返回0? - How can I write an android cordova plugin that will return 1 if bluetooth is on and 0 otherwise? 无法使用cordova-plugin-file插件从android目录中读取文件 - Can't read a file from android directory using cordova-plugin-file plugin 如何使用cordova-plugin-file在cordova应用程序之间共享数据? (Android,IOS) - How to share data between cordova apps using cordova-plugin-file? (Android, IOS) 如何在不使用应用商店的情况下使用类似于iOS设备的清单文件来部署android应用? - How can I deploy android apps using a manifest file, similar to iOS devices, without using an app store? 使用 Cordova android 应用程序,我如何访问(读/写)Win10 设备共享文件夹中的 SQLite 文件? - With Cordova android app, how I can access (read/write) SQLite file at shared folder in Win10 device? 如何通过 Android/iOS 上的不同线程同时读取和写入一个文件? - How can I read & write one file at same time by different threads on Android/iOS? 在HTML5 Cordova上保存gamestate。 无法使用cordova-file-plugin进行读写 - Saving gamestate on HTML5 Cordova. Can't read/write with cordova-file-plugin 如何使用cordova添加插件? - how can I add plugin using cordova? 无法使用cordova -file插件查看存储在Android设备上的文件 - Not seeing files that are store on the Android device using cordova -file plugin 如何在Cordova应用程序中使用JavaScript读写XML文件? - How can I read and write XML file with JavaScript in my cordova app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM