Problem with Adobe AIR file stream for Android

I tried to check the hit code on an Android device, but I could not see any data in the text fields. This code works well on AIR Desktop , but not on Android . Difference between AIR FileStream function on Desktop and Android ? What is the best cross-platform code for saving files and reading records?

This code runs on the 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(); 
+6
source share
2 answers

This is the right way, we use it for an AIR application (Desktop, Android, iOS) to store user data on a device / PC, File.applicationStorageDirectory . So your code is cross-platform, if it doesn't work on Android, you probably have another problem, but your code looks fine.

To read / write data to File.applicationStorageDirectory , you also do not need explicit permissions in the application manifest.

+4
source

If your version is Android 6.0 or later, you must request permissions such as files or camera at runtime.

Make sure you have AIR 24.0, with the compiler option -swf-version = 35, and do something like:

var licenseCheck: 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 information, such as camera resolution.

https://helpx.adobe.com/flash-player/release-note/fp_24_air_24_release_notes.html

Hope this helps.

0
source

Source: https://habr.com/ru/post/1014591/


All Articles