flex extract resources from swc to application directory - apache-flex

I have flex library projects with resources that I need to have in every aplication directory that uses this swc. How can I build my flex library project for it?

In the flex library write an utility class that stores all resources on the application directory.
To save files here's a small code snippet that should get you on your way:
var file:File = File.applicationStorageDirectory.resolvePath("new-file.png");
var fileStream:FileStream = new FileStream();
fileStrean.open(file, FileMode.WRITE);
fileStream.writeBytes(data, 0, data.length);
fileStream.close()

Related

Javafx WebEngine load html file from jar

I am building a project and need to use WebEngine class. So I have 2 projects, MainA and SecondB. The SecondB project is in dependency to MainA, so after building it SecondB becomes a jar file in the MainA libs folder.
Now. I need to open a abc.html file that is under SecondB resources. When testing it locally it works, when building an app and deploying it on server it fails (probably because it is in SecondB jar file). So the code I am using is:
WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();
ClassLoader classLoader = getClass().getClassLoader();
String s = classLoader.getResource("subfolder/abc.html").toExternalForm();
webEngine.load(s);
The method classLoader.getResource("subfolder/abc.html").toExternalForm();returns a normal url when running the code localy and something like:
file:jar:C:/Something/MainA/libs/SecondB.jar!/subfolder/abc.html
Do you have any ideas how to load this file from a jar? I tried several options I found on SO, but without success
You can create a "resources" package and copy your files to this package and for accessing your file path for example an html you can use this: getClass().getResource("/resources/abc.html").toURI().toString() this provides to get your files url in your created jar. Hope it is useful.
To me, I did this
String fileloc = "/docs/help/userguide.html";
...
String fullLink = getClass().getResource(fileloc).toExternalForm();
...
I store the html file inside the resources folder. e.g./resources/docs/help/userguide.html
As I run the compiled jar, it has no problem being loaded up by javafx webengine.
I think you are almost there, you can read the url via InputStream into String, and use WebEngine.loadContent to load the String content, provided you only want to display single page. If your page reference to other files (javascript/css), it will not work.
So, it is better to this, while your JavaFX app is started, you copy out the whole resource folder into your local file system, then use the WebEngine to load the HTML file, this should work better.

How to use pre-designed SQLite in UWP project?

I have a SQLite database from another project and I want to use it in a UWP application. But I don't know how to import this database to the project and!
I can create a new Database and use it but I don't know how to copy database file from project. I use from SQLite.Net-PCL nuget package.
For how to access an exist file, there are two locations that all apps can access. Details please reference the file-access-permissions.
One is Application install directory. As #Henk Holterman said, you can import your existed database file into your project by right-click one folder and select Add->Existing item to add your database file to the project. Pay attention the file's Build action property need to be set to content. Details please see the following picture.
Suppose you already had a database file Sun.db added to the Assets folder, and now you can connect to it by the following code( use the SQLite.Net-PCL Nuget Package).
path = Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, #"Assets\Sun.db");
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path))
{
}
But this folder is read only. Another location is Application data locations which can read/write. You can copy the database file from install directory to the application data directory for using. The following code example is for connecting a database file that in the local folder.
StorageFile file;
try
{
file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("Sun.db");
}
catch
{
StorageFile Importedfile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Sun.db"));
file = await Importedfile.CopyAsync(Windows.Storage.ApplicationData.Current.LocalFolder);
}
path = file.Path;
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path))
{
conn.CreateTable<User>();
}

Flex Image Source Server Side Flash and Air

I'm trying to run my flex application in the air runtime instead of flash runtime. It seems to work perfectly except the images. Adobe Air runtime tries to load them. Is there a way to change the root adresses for Image to server side? If possible I'd like to use the same code for flash runtime and air runtime .. "single codebase ;-)"
var icon:Image = new Image();
icon.source = "images/test.png";
regards
cyrill
Typically I would simply package the assets into the AIR app. That way the relative paths would be valid both in the web app and the desktop app. However, since you pointed out in the comments that we're talking 10000 images you'll have to find another solution.
What you need is a variable that is configurable for each type of project. The final code to access your images should look like:
var icon:Image = new Image();
icon.source = rootUrl + "/images/test.png";
That rootUrl may be "" for the web app, and "http://www.mydomain.com" for the desktop app. Or it could be the absolute path in both cases. It doesn't matter: we don't want to hardcode that URL into our application.
Create a .properties file (or XML, or JSON; whatever configuration file you like) that contains the value for rootUrl and read that into your application model. This configuration file can be packaged into the AIR app.
A .properties file will look like this:
#myapp.properties
rootUrl=http://www.mydomain.com
For reading the file, you could use AIR's file streaming capabilities, but I suggest you load it the old-fashioned way with a URLLoader: this way it'll work both in the web and the desktop app.

Copy DB file From Flex AIR bindebug Folder To Any Folder Programatically

I have a db file Resided in Flex Air Bindebug Folder,Here i want to Move/save this db File at another location let say In mydocument/or any Folder, I am not able to do it's programatically..Please help me out here.
Thanks in advance.
Assuming mydbfile.db is the file in your Application Directory:
var fileToCopyFrom:File = File.applicationDirectory.resolvePath("mydbfile.db");
var fileToCopyTo:File = File.documentsDirectory("Copy of mydbfile.db");
fileToCopyFrom.copyTo(fileToCopyTo as FileReference);
This would copy the file to your Documents directory. These are all basic functions weill documented in the AS3 Reference for flash.filesystem.File.
Are your trying to do this via actionscript or in FlashBuilder/Eclipse?
If your trying to move around in The IDE then use ANT.

Can you do binary file input and out in Flex 4?

I want to write a logging system to log errors but I want the format of the file to be binary. Can you write and/or read binary files in Flex 4? (actionscript to be more precise)
Yep, you can write binary files using the ByteArray class, and save them using the FileReference class (you need to target FP10).
For example, to write an XML file to binary, you simply do:
var ba:ByteArray = new ByteArray;
ba.writeUTFBytes( myXML ); // myXML is an XML object
ba.compress(); // this will compress the ByteArray, saving you a ton in size
To save it, you run it through the FileReference API:
var file:FileReference = new FileReference;
file.save( ba, "myXML.xml" );
The only drawback with this is that FileReference.save() has to be called from a user action (mouse click/keyboard press). Going through the FileStream class in AIR gives you more options. FileReference can also be used to upload files to the server. Check out the docs to get more of an idea: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html
Alternatively, you can save your ByteArray info through a SharedObject (http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/SharedObject.html) without too much hassle, or do it as Flextras suggested and run it through a server script.
ByteArray docs: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/ByteArray.html
Generally, a Flex web based app has very limited ability to write or read files that are local to a machine. If you're using AIR you can use the File API and the FileStream API. It should be possible to read/write binary files with that.
If you're building a web based app; then you can send data to the server, and let that save files. I believe most application servers should support reading and writing of binary files.

Resources