6

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();
Mohammad Samir
  • 211
  • 1
  • 8

2 Answers2

4

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.

fsbmain
  • 5,267
  • 2
  • 16
  • 23
  • I selected all permission in settings and compiled again. now it works. – Mohammad Samir Jan 31 '17 at 12:58
  • @MohammadSamir you don't need **all permissions**. Some people are turned off by an app that needs to [unnecessarily] access everything (SD card, phone contacts, GPS, etc...why?), try allowing "`read/write to device storage`" if your file is not saved/loaded within `File.applicationStorageDirectory`... – VC.One Jan 31 '17 at 21:57
0

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.

johnny2hats
  • 86
  • 1
  • 4
  • johnny2hats, could you add to your answer? What you gave does work, but it's for File, which does have a requestPermission() method. The original question was about FileStream, and that doesn't have requestPermission(). Under Android 6 how would you get permission to use FileStream? – Colin Holgate Dec 29 '18 at 14:17