【问题标题】:Create a Directory in PhoneGap Application在 PhoneGap 应用程序中创建目录
【发布时间】:2012-03-09 17:39:08
【问题描述】:

试图弄清楚如何使用 PhoneGap 在文件系统上创建目录。

我想为我的 PhoneGap 应用程序创建一个目录,这样我就可以存储用户在那里创建的图像并将它们重新加载到应用程序中。

【问题讨论】:

    标签: mobile cordova filesystems directory


    【解决方案1】:

    这就是你的做法:

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onRequestFileSystemSuccess, null); 
    
    function onRequestFileSystemSuccess(fileSystem) { 
            var entry=fileSystem.root; 
            entry.getDirectory("example", {create: true, exclusive: false}, onGetDirectorySuccess, onGetDirectoryFail); 
    } 
    
    function onGetDirectorySuccess(dir) { 
          console.log("Created dir "+dir.name); 
    } 
    
    function onGetDirectoryFail(error) { 
         console.log("Error creating directory "+error.code); 
    } 
    

    在 iOS 上,此脚本将在 Applications/YOUR_APP/Documents/ 中创建一个名为“example”的目录

    【讨论】:

    • 是的,这行得通。我正要回答我的问题,因为我也发现了这一点。有趣的是,直到我将其发布到 SO 上才找到答案,然后我立即找到了。
    • @JohnSonmez 你用什么代码来保存图片?
    • @titouan-de-baileul 是否需要一些外部资源
    • 似乎对我不起作用。我在 config.xml 中添加了<preference name="AndroidPersistentFileLocation" value="Compatibility" /><gap:plugin name="org.apache.cordova.file"/><gap:plugin name="org.apache.cordova.file-transfer"/>,但它仍然无法正常工作:(
    【解决方案2】:

    此代码适用于我(基于版本 7 Cordova 文档 - https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/index.html):

    window.resolveLocalFileSystemURL('cdvfile://localhost/persistent/',
      function fileEntryCallback(fileEntry) {
        fileEntry.getDirectory('my_directory', { create: true });
      }
    );
    

    如果上面的代码不起作用,您可能需要重新安装文件和文件传输插件。像console.log(window.resolveLocalFileSystemUrl) 这样的命令是测试插件是否正常工作的一种方法。

    此外,如果您使用的是内容安全策略 (CSP),请确保您的 default-src 允许 cdvfile: 路径:

    default-src cdvfile:

    【讨论】:

      猜你喜欢
      • 2013-05-18
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-18
      • 2011-07-17
      • 1970-01-01
      相关资源
      最近更新 更多