MgFrobozz
MgFrobozz

Reputation: 146

Moving from cordova 4.3.0 to 6.0.0 File plugin?

I've got a cordova app that was developed on cordova 4.3.0 for android, and I'm trying to build the ios version. The cordova on my OS X system is 6.0.0, and I ran into problems removing and reinstalling 4.3.0, so I'm trying to move my design forward to 6.0.0 instead. In doing that, I found my existing plugins weren't compatible with 6.0.0, so I removed them and reinstalled:

cordova-plugin-console 1.0.2 "Console"
cordova-plugin-device 1.1.1 "Device"
cordova-plugin-file 4.1.0 "File"
cordova-plugin-file-transfer 1.5.0 "File Transfer"
cordova-plugin-media 2.1.0 "Media"

Additionally, I was prompted to install android 6.0 (API 23) because the existing installation was no longer compatible after the changes above.

Now I'm attempting to get the android version working again. Among other problems too numerous to list, a directory read on my app directory (/storage/sdcard0/MyDir) returns 0 files, at the same time 'adb shell' reported that it contained 9 files (recorded by the cordova 4.3.0 build). The app (code below) was still able to record audio files (using m_directory.getFile), and the files appeared in that directory (according to 'adb shell'), but m_directory.readEntries couldn't find them, only the files that had been recorded during that session.

If I change the directory name to "MyDir2", the new directory is never created (even though create=true), but the media object does create the file /storage/sdcard0/tmprecording.3gp, and then delete it when the recording is done. In other words, it can write a file to the existing directory from 4.3.0, but cannot create a new directory, and for both the existing directory and the new directory, readEntries won't include any files it hasn't created in that session.

Update 2015-02-07: I found this gem buried in the cordova docs. Go to https://cordova.apache.org/docs/en/6.0.0/cordova/plugins/pluginapis.html, click on FileSystem, which shows "The (now-defunct) Directories and System extensions". I think this means that cordova has walked away from at least part of plugins/cordova-plugin-file; it still references the docs at w3.org, but doesn't indicate clearly which parts won't work any more. It still allows installing the plugin, and it still allows usage of all of the functions, but at least some of them are broken with no warning messages, which certainly appears to be the case with file_system.root.getDirectory. Other parts (eg, DirectoryEntry) have apparently been preserved.

The change bull-dozed the foundation under my house (app), so I'm starting again with

var dir_path = cordova.file.externalDataDirectory;
m_directory = window.resolveLocalFileSystemURL(dir_path, 
    on_resolve_directory_ok, on_resolve_directory_error);

which is supposed to provide the directory entry "to put app-specific data files on external storage". The audio files I'm recording appear in that directory, but they remain only as long as the app is running. Shutting it down and restarting it erases all of the files. It acts like it's using temporary memory instead of persistent memory, but I don't know how to fix that.

Update 2015-02-08: It appears that running "cordova run android --target nnnnnnnnnnnn" (target number for my phone obscured) clears the contents of the directory /storage/sdcard0/Android/data/com.MyDomain.MyApp/files (same as cordova.file.externalDataDirectory). I think the files were persistent, but something that cordova does during the reload deleted them. If I exit the app and then restart it (without using "cordova run"), then the files remain. If I exit the app and then use "cordova run", the files disappear.

The other thing that "cordova run" erases are the window.localStorage items, which is a real pain because I have to hand-enter them every time I debug my app. https://www.w3.org/TR/webstorage/ is pretty clear that this should not happen: User agents should expire data from the local storage areas only for security reasons or when requested to do so by the user. User agents should always avoid deleting data while a script that could access that data is running.

My revised question is: How can I use "cordova run android" without deleting all files in cordova.file.externalDataDirectory and all settings in window.localStorage?

Old code (using window.requestFileSystem(LocalFileSystem.PERSISTENT, ...) follows.

var m_directory = null;

function on_device_ready() 
{
    create_directory_entry();
}

function create_directory_entry()
{
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, 
        on_file_system_ok, on_file_system_error);
}

function on_file_system_ok(file_system) 
{
    console.log("on_file_system_ok: file_system.name " + file_system.name);
    var attribs = {create: true, exclusive: false};
    file_system.root.getDirectory("MyDir", attribs, on_dir_ok,
        on_dir_error);
}

function on_dir_ok(dir_entry)
{
    console.log("on_dir_ok: dir_entry.fullPath " + dir_entry.fullPath);
    m_directory = dir_entry;
    list_files(dir_entry);
}

function list_files(dir_entry)
{
    var dir_reader = dir_entry.createReader();
    dir_reader.readEntries(create_file_list, on_dir_read_error);
}

function create_file_list(entries)
{
    console.log("create_file_list: entries.length=" + entries.length);
}

Upvotes: 2

Views: 451

Answers (1)

traildex
traildex

Reputation: 358

May be related to issue CB-10157 Application uninstalls before installing, clearing localStorage and databases

the application ALWAYS uninstalled before being installed. This means that the localStorage and any databases associated with the application are CLEARED. This is a major problem for developing applications which rely on storing some data between installations.

https://issues.apache.org/jira/browse/CB-10157

A comment on https://issues.apache.org/jira/browse/CB-10585

It has been fixed on cordova-android 5.2.0-dev

I could not find any info on when 5.2.0 will release

cordova platform update [email protected]

nor

cordova platform update [email protected]

currently work, so I guess it's a waiting game or install an older version

Upvotes: 2

Related Questions