Adobe AIR Android FileStream issue
Asked Answered
P

2

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();
Potto answered 31/1, 2017 at 10:59 Comment(0)
P
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.

Peaceable answered 31/1, 2017 at 12:50 Comment(2)
I selected all permission in settings and compiled again. now it works.Potto
@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...Tetragrammaton
T
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.

Trailer answered 22/2, 2017 at 14:56 Comment(1)
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?Maladjusted

© 2022 - 2024 — McMap. All rights reserved.