Adobe AIR Android FileStream issue - actionscript-3

I tried to test blow code on Android device but i couldn't see any data in text fields. This code works well on AIR Desktop but in Android no. Is it difference between AIR FileStream function on Desktop and Android? Whats best cross platform code to save files and read write?
This code works on Adobe Animate CC Android Emulator.
import flash.filesystem.*;
var prefsFile:File;
[Bindable] var prefsXML:XML;
var stream:FileStream;
function appStart():void
{
prefsFile = File.applicationStorageDirectory;
prefsFile = prefsFile.resolvePath("preferences.xml");
readXML();
}
function readXML():void
{
stream = new FileStream();
if (prefsFile.exists) {
stream.open(prefsFile, FileMode.READ);
processXMLData();
}
else
{
saveData();
}
}
function processXMLData():void
{
prefsXML = XML(stream.readUTFBytes(stream.bytesAvailable));
stream.close();
trace(prefsXML.Data1);
trace(prefsXML.Data2);
trace(prefsXML.Data3);
txt_D1.text = prefsXML.Data1;
txt_D2.text = prefsXML.Data2;
txt_D3.text = prefsXML.Data3;
}
function windowClosingHandler(event:Event):void
{
saveData();
}
function saveData():void
{
createXMLData();
writeXMLData();
}
function createXMLData():void
{
prefsXML = <preferences/>;
prefsXML.Data1 = 1;
prefsXML.Data2 = 2;
prefsXML.Data3 = 3;
}
function writeXMLData():void
{
var outputString:String = '<?xml version="1.0" encoding="utf-8"?>\n';
outputString += prefsXML.toXMLString();
outputString = outputString.replace(/\n/g, File.lineEnding);
stream = new FileStream();
stream.open(prefsFile, FileMode.WRITE);
stream.writeUTFBytes(outputString);
stream.close();
}
appStart();

That's the correct way, we use it for AIR app (Desktop, Android, iOS) to store user data on device/PC, File.applicationStorageDirectory as well. So your code is cross-platform, if it does not work on Android, you probably have some other issue, but your code looks fine.
To read/write data to File.applicationStorageDirectory you don't need any explicit permissions in app manifest as well.

If your Android version is 6.0 or more recent, you need to request permissions such as files or camera at runtime.
Ensure you have AIR 24.0 , with compiler option -swf-version=35, and do something like:
var permissionCheck : File = new File( "test") );
permissionCheck.addEventListener(PermissionEvent.PERMISSION_STATUS , function permissionStatusHandler( e : PermissionEvent ) :void
{
permissionCheck.removeEventListener(PermissionEvent.PERMISSION_STATUS , permissionStatusHandler);
if(e.status == PermissionStatus.GRANTED)
{
// save your file
}
else
{
showPermissionError();
}
});
try
{
permissionCheck.requestPermission();
}
catch(error : Error)
{
}
See the AIR release notes for more info such as handling the camera permission.
https://helpx.adobe.com/flash-player/release-note/fp_24_air_24_release_notes.html
Hope this helps.

Related

Move files from applicationStorage to Documents in app

I've got an app that, since 5 years now, that displays an offline map by reading from a folder embed in it ("assets").
Since Android 11, it's impossible to read from ApplicationStorage (Error #3001: File or directory access denied), so I'm trying to copy the folder from applicationStorage to "Documents".
What I did :
source = File.applicationDirectory.resolvePath("assets/maps");
destination = File.documentsDirectory.resolvePath("Documents/www");
source.addEventListener(Event.COMPLETE, onMapCopyComplete);
source.copyToAsync(destination, false);
function onMapCopyComplete(e: Event): void {
source.removeEventListener(Event.COMPLETE, onMapCopyComplete);
log("onMapCopyComplete()");
}
I've got a return onMapCopyComplete() but when I'm looking in InternalStorage/Documents of my phone I've got the folders but all are empty... None of the files were copy..
PrintScreen computer vs phone
To read the files, here's what I'm doing :
function startMapsView()
{
var indexFile:File = File.applicationStorageDirectory.resolvePath("www/index.html");
if (!indexFile.exists)
{
log("startMapsView() Index file not found, Please check www/index.html");
return;
}
// Create StageWebView
stageWebView = new StageWebView(isMobile); // Set to TRUE for System's NativeWebView, FALSE is for AIR's WebView
stageWebView.stage = stage;
stageWebView.viewPort = new Rectangle(0, iOSStatusBarHeight + headerBarHeight, deviceStageSize.width, deviceStageSize.height - (iOSStatusBarHeight + headerBarHeight + footerBarHeight));
stageWebView.addEventListener(flash.events.Event.COMPLETE, onStageWebViewLoaded);
stageWebView.addEventListener(flash.events.ErrorEvent.ERROR, onStageWebViewError);
stageWebView.addEventListener(LocationChangeEvent.LOCATION_CHANGING, onStageWebViewLocationChanging);
// Load Map URL
stageWebView.loadURL(mapsURL);
}
And mapsURL is define by :
function setMapsURL(doNotEnableButtons: Boolean = false): void {
var indexFile: File = File.documentsDirectory.resolvePath("Documents/www/index.html");
trace("indexFile url is = "+indexFile.url);
if (!indexFile.exists) {
log("setMapsURL() Index file not found, Please check www/index.html");
return;
}
var assetsDir: File;
if (!useOnlineMaps) {
assetsDir = new File(destination.resolvePath("index.html").nativePath);
} else {
assetsDir = new File(destination.resolvePath("onlineMaps.html").nativePath);
mySavedData.data.onlineMapChoosen = false;
}
mapsURL = assetsDir.url;
log("setMapsURL() " + mapsURL);
if (!doNotEnableButtons) enableMapButtons();
}

Orientation of Forge viewable generated on Design Automation in Forge Viewer not matching orientation in Inventor

I'm exporting SVFs from a model using the design automation API. With some models, the orientation in the Viewer of the viewable does not match the orientation in Inventor.
How do I correct this so that all models come out with their Viewer orientation matching the input Inventor model?
The following code is where the SVF is exported. A blog post on this functionality would be helpful.
private string SaveForgeViewable(Inventor.Document doc) {
string viewableOutputDir = null;
using(new HeartBeat()) {
//LogTrace($"** Saving SVF");
try {
TranslatorAddIn oAddin = null;
foreach(ApplicationAddIn item in inventorApplication.ApplicationAddIns) {
if (item.ClassIdString == "{C200B99B-B7DD-4114-A5E9-6557AB5ED8EC}") {
//Trace.TraceInformation("SVF Translator addin is available");
oAddin = (TranslatorAddIn) item;
break;
}
else {}
}
if (oAddin != null) {
//Trace.TraceInformation("SVF Translator addin is available");
TranslationContext oContext = inventorApplication.TransientObjects.CreateTranslationContext();
// Setting context type
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism;
NameValueMap oOptions = inventorApplication.TransientObjects.CreateNameValueMap();
// Create data medium;
DataMedium oData = inventorApplication.TransientObjects.CreateDataMedium();
Trace.TraceInformation("SVF save");
var workingDir = Path.GetDirectoryName(doc.FullFileName);
var sessionDir = Path.Combine(workingDir, "SvfOutput");
// Make sure we delete any old contents that may be in the output directory first,
// this is for local debugging. In DA4I the working directory is always clean
if (Directory.Exists(sessionDir)) {
Directory.Delete(sessionDir, true);
}
oData.FileName = Path.Combine(sessionDir, "result.collaboration");
var outputDir = Path.Combine(sessionDir, "output");
var bubbleFileOriginal = Path.Combine(outputDir, "bubble.json");
var bubbleFileNew = Path.Combine(sessionDir, "bubble.json");
// Setup SVF options
if (oAddin.get_HasSaveCopyAsOptions(doc, oContext, oOptions)) {
oOptions.set_Value("GeometryType", 1);
oOptions.set_Value("EnableExpressTranslation", true);
oOptions.set_Value("SVFFileOutputDir", sessionDir);
oOptions.set_Value("ExportFileProperties", false);
oOptions.set_Value("ObfuscateLabels", true);
}
oAddin.SaveCopyAs(doc, oContext, oOptions, oData);
LogTrace($ "** Saved SVF as {oData.FileName}");
File.Move(bubbleFileOriginal, bubbleFileNew);
viewableOutputDir = sessionDir;
}
}
catch(Exception e) {
LogError($ "********Export to format SVF failed: {e.Message}");
return null;
}
}
return viewableOutputDir;
}
we have met this issue too, this is our setup of SVF output which respects the ground of your design:
oOptions.set_Value("EnableExpressTranslation", false);
oOptions.set_Value("ExportFileProperties", true);
oOptions.set_Value("ObfuscateLabels", false);
For full code you can see our new sample app repository https://github.com/Developer-Autodesk/forge-configurator-inventor/blob/master/AppBundles/CreateSVFPlugin/CreateSvfAutomation.cs#L96

How to simulate Geolocation service AS3

I'm creating an app that is using Geolocation servioce on phone o send location/time data over the internet. And that is working just fine. Problem is that I cannot test it in Flashdevelop, cause Geolocation is not supported, so I have to upload every time new code and test it on phone. Is there any way to simulate Geolocation service in Flashdevelop? Or generally, on desktop PC?
Sorry for bothering ... I found out... quite simple answer was..
private function startLocating():void {
var myLat:Number=44.2343;
var myLong:Number=20.9432;
_geo = new Geolocation();
_geo.addEventListener(GeolocationEvent.UPDATE, Handler1 );
if (Geolocation.isSupported) {
if (!_geo.muted) {
_geo.setRequestedUpdateInterval(1000);
} else {
trace ("Location service not turned on.");
}
} else {
/* this code is working when Geolocation is not supported */
_geo.dispatchEvent(new GeolocationEvent(GeolocationEvent.UPDATE,false,false,myLat,myLong));
}
}
private function Handler1(ev:GeolocationEvent):void {
var latitude:Number = ev.latitude;
var longitude:Number = ev.longitude;
.....
}

Compiling Flex Air apps for PC, Android and Mac from one codebase?

I would like to use Air to build for PC, Mac, Android, Ios.
Is it possible to do this from a single code base as Adobe Suggests.
Or will I need to maintain 4 separate builds?
Some guidance would be appreciated.
Regards
C
I have been able to maintain a single code base to date. I have done things like the following:
private function getHostName() : void
{
if (NativeProcess.isSupported)
{
var OS : String = Capabilities.os.toLocaleLowerCase();
var file : File;
if (OS.indexOf('win') > -1)
{
// Executable in windows
file = new File('C:\\Windows\\System32\\hostname.exe');
}
else if (OS.indexOf('mac') > -1 )
{
// Executable in mac
}
else if (OS.indexOf('linux'))
{
// Executable in linux
}
var nativeProcessStartupInfo : NativeProcessStartupInfo = new NativeProcessStartupInfo();
nativeProcessStartupInfo.executable = file;
var process : NativeProcess = new NativeProcess();
process.addEventListener(NativeProcessExitEvent.EXIT, onExitError);
process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutput);
process.start(nativeProcessStartupInfo);
process.closeInput();
}
}
private function onOutput(event : ProgressEvent) : void
{
var strHelper : StringHelper = new StringHelper();
formStationID.text = event.target.standardOutput.readUTFBytes(event.target.standardOutput.bytesAvailable);
formStationID.text = strHelper.trimBack(formStationID.text, "\n");
formStationID.text = strHelper.trimBack(formStationID.text, "\r");
}
private function onExitError(event : NativeProcessExitEvent) : void
{
}
To take care of native calls. Other than the native calls I have found few things that can't be written generically, but of those, the above approach should work with any part of the code set as well.

HTML5 FileReader alternative

I need some help with HTML5. I have a script that loops through all the uploaded files and gets each file details. Currently I am using HTML5 techniques that include FileReader. The FileReader function only works in Chrome and Firefox, so I am looking for an alternative which will work in all of the other browsers.
I saw the Stack Overflow question Flash alternative for FileReader HTML 5 API, but I wasn't able to figure how to use this Flash thing, and aren't there any other solutions so I can loop through all of the uploaded files and get each file details (which will work in Safari and Internet Explorer)?
Ended up not using FileReader at all, instead I looped through event.files and got each file by files[i] and sent an AJAX request by XHR with a FormData object (worked for me because I decided I don't need to get the file data):
var xhrPool = {};
var dt = e.dataTransfer;
var files = (e.files || dt.files);
for (var i = 0; i < files.length; i++) {
var file = files[i];
// more code...
xhrPool[i] = getXMLHttpRequest();
xhrPool[i].upload.onprogress = uploadProgress;
initXHRRequest(xhrPool[i], i, file);
data = initFormData(i, file);
xhrPool[i].send(data);
}
function initFormData(uploaded, file) {
var data = new FormData();
data.append(uploaded, file);
// parameters...
return data;
}
function uploadProgress() {
// code..
}
function initXHRRequest(xhr, uploaded, file) {
// code... onreadystatechange...
xhr.open("POST", "ajax/upload.php");
xhr.setRequestHeader("X-File-Name", file.name);
}
function getXMLHttpRequest()
{
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
}
else {
try {
return new ActiveXObject("MSXML2.XMLHTTP.3.0");
}
catch(ex) {
return null;
}
}
}
Safari was the first one to actually implement the HTML5 file API, and there are several demos. Andrea Giammarchi has a nice description on his blog. There are several frameworks to handle this as well which also have fallbacks for Internet Explorer. Fancyupload is one that comes to mind.