Skip navigation
Currently Being Moderated

Working with File objects in AIR

Aug 28, 2012 9:18 AM

 
Replies
  • Currently Being Moderated
    Aug 28, 2012 9:18 AM   in reply to Community Help

    I am try to make an application for android. I have 2 .xml file. which dynamically created after installing app as per user requirement. I used to save  file path is File.applicationStorageDirectory.resolvedPath.

    But for 1 file it works not for 2nd one. Its throwing error. even 2 more static xml also not loading if i used path 2 times for dynamic xml. Plz help me out

     

     

    Thanks in Advance

    Rachita

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 28, 2012 9:32 AM   in reply to Rasmy2012

    Please post the relevant code you used to try to load the XML.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 28, 2012 10:12 PM   in reply to sinious

    FOR LOADING XML

    var file1:File = File.applicationStorageDirectory.resolvePath("myFav.xml");

    var fileStream1:FileStream = new FileStream();

    fileStream1.open(file1, FileMode.READ);

    var imageList1:XML = XML(fileStream1.readUTFBytes(fileStream1.bytesAvailable));

    fileStream1.close();

     

    FOR SAVING XML

     

    function saveXML(myXML:XML):void

    {

         var file2:File = File.applicationStorageDirectory.nnativePath("myFav.xml");

         fileStream1.open(file2, FileMode.WRITE);

         fileStream1.writeUTFBytes(myXML.toXMLString());

         fileStream1.close();

    }

     

     

     

    Above mention is my code. it works for xml but the same if i am using for the second xml it throwing error #30003 file or directory does not exists.

    So just plz check it out once.

     

    Thanks in Advance.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 28, 2012 10:41 PM   in reply to Rasmy2012

    Getting below error also

     

    SecurityError: fileWriteResource

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 29, 2012 7:16 AM   in reply to Rasmy2012

    In that code you're re-using a FileStream. You may have better luck re-creating your fileStream object every time. After you finish writing null out the file1/2 and the fileStream so the next time it fires off these fresh objects are re-created. Therefore you can't get into any "file is in use therefore you can't overwrite it" bugs.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 29, 2012 9:06 AM   in reply to sinious

    But I did the same for 1st xml file name is "theme.xml". It works properly. When i run my code theme.xml saved in my local storage and i can change it through the application bt at the same time second xml have not save in my local storage.. I did the which you mentioned above. iused seprate file stream for all bt its not working...

     

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 29, 2012 9:32 AM   in reply to Rasmy2012

    Yes I see you're using the same FileStream object to manage both files and that's where I think the most appropriate place to adjust is.

     

    There's a big difference between the two things you're doing. One is opening a file for FileMode.READ. The other is FileMode.WRITE. You're getting a security fileWriteResource error. That tells me either the FileStream being re-used might not work well with Android or you really don't have access to writing to the filesystem.

     

    In your Android manifest XML file did you request permission to write to storage? Here's a picture of the relevant section taken from this adobe page:

     

    http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/air/articles/packaging-air-apps-android/fig_10.jpg

     

    You need to make sure you gave yourself this permission (uncomment it out if it is commented):

     

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

     

    Otherwise you are not allowed to write files.

     

    If you have that permission and you're creating a new FileStream object as you should be, are you getting a different error?

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 29, 2012 10:28 PM   in reply to sinious

    Yes I agreed that There are two thing read and write a xml but i am reading the and writing both xml.. it is not like that one xml is static and one is dynamic. whole code are same for read and write.

    so i ould save only one. second xml created when i run the code bt it is blank and i give static path for second xml it works . Bt i cant do that.. it should be saved in by default local path ....

    I could not getting, what is the wrong with the code. I cross checked 100 of times

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 30, 2012 7:33 AM   in reply to Rasmy2012

    As long as you have write permission then I see nothing immediate wrong. You keep mentioning XML. XML is just text, regardless. This Adobe article on opening/editing/saving text using the FileStream class should give you plenty of insight on every part of the process:

     

    http://www.adobe.com/devnet/air/flash/quickstart/articles/building_tex t_editor.html

     

    Make sure you're checking IOErrorEvents for more information on your issues of course.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 31, 2012 4:03 AM   in reply to sinious

    can i have list of package which has to be import for android for touch event.

     

    Thanx

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 3, 2012 8:15 AM   in reply to sinious

    Hey Sorry to disturb you again. Actually i stuck with one more problem.

    I used TransformGestureEvent for both zoom and pan. See i have one image that i can zoom. i maintain the aspect ratio also while zooming, i can pan also but while i am moving the object, it moves easily so that if i move bottom it can be move till top and layer under that image  become visible that's i dont want. Even i restricted zoom out can't be less from 1028 X 750.this is my stage size so image it should maintain the axis - x between the 0-1280 and y-0-750.

    Plz help.

    Thanks in advance. 

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 4, 2012 8:33 AM   in reply to Rasmy2012

    I'm not really sure what your question is. What it seems to be is your gestures are allowing your content to be sized outside your desired dimensions.

     

    All I can tell you is you should always be multiplying via the value returned by the gesture event and then run the values the scale would result in though your own set of tests. If it's larger, smaller or in a position you don't want you simply ignore the event. That's always how I've kept my content in check and where I want it.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 5, 2012 4:40 AM   in reply to sinious

    Hey Sinious, Yes i figured out.Thanks alot.  but the thing is that how can i get value of touchpointid.

    On one fingure touch i want touch and drag and two fingure touch or pinch i want zoom.

    we cant merge multitouchgesture and multitouchpoint as per forum, so every time i have to remove and add  event listener depending on touch type.

    so what to do?

     

    Thanks

     

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 5, 2012 6:36 AM   in reply to Rasmy2012

    How I do it is handle dragging myself manually using a standard MouseEvent. On MOUSE_DOWN I start either pay attention to MOUSE_MOVE or startDrag(rect) on the clip and handle a drag in that manner. That's how you'd always do it before in Flash so a single Touch for something Flash itself has always supported is probably why they don't work together.

     

    I use MOUSE_MOVE more than startDrag as you can do more sophisticated things, albeit it can be expensive so keep the calculations as simple as possible. An example of a random MOUSE_DOWN handler setup that only moves horizontal (X axis):

     

    // hold current position in lightweight ints (whole pixels only)

    var startX:int;

     

    // weak reference listener

    someObj.addEventListener(MouseEvent.MOUSE_DOWN, _handleMouseDown, false, 0, true);

     

    function _handleMouseDown(e:Event):void

    {

         // on initial MOUSE_DOWN, store starting value

         startX = stage.mouseX;

     

         // add MOUSE_MOVE and removal handler for movement

         stage.addEventListener(MouseEvent.MOUSE_MOVE, _handleMouseMove);

         stage.addEventListener(MouseEvent.MOUSE_UP, _handleRemoveMouseMove);

    }

     

    function _handleMouseMove(e:MouseEvent):void

    {

         // calculate

         var delta:int = int(e.localX - startX);

     

         // validate value, allow to go left -2000px and up to 0px

         if ((someObj.x - delta) > -2000)

         {

              someObj.x -= delta;

              startX = e.localX;

         }

         else if ((someObj.x - delta) >= 0)

         {

              someObj.x = 0;

              startX = e.localX;

         }

    }

     

    function handleRemoveMouseMove(e:MouseEvent):void

    {

         stage.removeEventListener(MouseEvent.MOUSE_MOVE, _handleMouseMove);

         stage.removeEventListener(MouseEvent.MOUSE_UP, _handleRemoveMouseMove);

     

         // code here once drag is complete, check position? do something?

    }

     

    I typed it quick so there could be a logic issue in there but there's a random handler that should allow an object to go left up to -2000px and it uses standard MouseEvent. I apply listeners for MOUSE_MOVE and MOUSE_UP to the stage to assure if the users finger gets off the object that's moving they will still stop listening. Customize as necessary but this is ultimately Flash's age old built in drag/move code.

     

    You could also calculate a valid Rect and use startDrag() to let Flash itself handle moving the object. I often want to do more things based on the position of the drag so I like to use a loop that keeps track of the position.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 5, 2012 7:18 AM   in reply to sinious

    Yes i did the same only i used TOUCH_BEGIN, TOUCH_END. I m applying this on an image. so while once i zoom image after that i can drag or move the image. But if immediately if want to zoom more in that case zoom gesture does not work. even i used MOUSE_UP and MOUSE_DOWN also. That's why i have to count touchpointid. and One more thing i defined all Touch_BEGIN and TOUCH_END event and function in Zoom Function itself to get the value of zooming size.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 5, 2012 11:01 AM   in reply to Rasmy2012

    If you're using MouseEvent to drag the object as shown above then you should be free and clear to use the zoom gesture to handle zooming. The zoom gesture returns a simple scale property you'd use to resize the image. You would't be using TOUCH_BEGIN/TOUCH_END at all.

     

    The MouseEvent pan code would need to take the scale into consideration for its pan calculations of course.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 6, 2012 3:35 AM   in reply to Community Help

    Thanks Sinious. My basic issue has been resolved for zoom with drag..

    Only one thing is remaining that once i zoom an image and dragged after that if i again zoom out little bit the image background image seen automatically which i dont want, actully immediately width and height is not been set..

        

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 6, 2012 8:19 AM   in reply to Rasmy2012

    There's an event that fires off when the zooming occurs for the gesture. You're hand-calculating the zoom based on the scale factor returned in the event. You're also going to be responsible for validating and correcting the x/y position of the image at the same time. So in the function that handles scaling the image during zoom, just validate the position and adjust as necessary to keep the background image from showing through.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 7, 2012 8:45 AM   in reply to Community Help

    I read somewhere that in android 4.0 or later version on movie clip Touch event will not work

    see here :

    http://sree.cc/corona-sdk/limiting-touch-events-to-the-masked-portion- of-the-object

    http://cometkorea.tistory.com/156

     

    One more thing is there any option to scale everything according to device size.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 10, 2012 8:34 AM   in reply to Rasmy2012

    Because of their counter-parts (MOUSE_DOWN, MOUSE_MOVE, MOUSE_UP, CLICK) is why I would assume the touch events are removed. There's only one good reason for touch events, multiple touch points. Gestures can handle those for you however. I do little in the way of Android development at the moment but I've done multiple touch points in the past.

     

    Scaling works the same way as it does in flash, just setting the stage.scaleMode to EXACT_FIT (no aspect ratio considerd) or NO_BORDER (aspect ratio retained). The content will "stretch" to the device size. Vectors look fine during this but bitmaps often take a hit. There's no magic bullet for this. You really should read the available device space and make a dynamic interface rather than scaling.

     

    Game makers hate this and often just go with scaling. In this situation they typically make wide apps. They make sure the left and right side of their apps are "optionally seen" so on wide devices you see a bit more. As other devices narrow the edges are simply cropped off and unseen. It's a common game tactic but it requires you strictly keeping the vital controls and visuals inside a save margin.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 11, 2012 6:05 AM   in reply to sinious

    Thanks Sinious. It works.Every thing is working properly.

    Once again Thanks for your help

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 11, 2012 6:38 AM   in reply to Rasmy2012

    Glad you got it working properly. If you're all set can you please mark it answered so we can filter unanswered questions.

     

    You're welcome and good luck!

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 12, 2012 12:57 AM   in reply to sinious

    Yes Of course Sinious. But where i have to mark? i dint get option to mark "answered"

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 12, 2012 5:09 AM   in reply to Rasmy2012

    Under each reply you can mark the response that solved your problem as correct or other replies as helpful. That moves the answer right below your question so others with the same question can see the answer easily.

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 22, 2012 7:10 AM   in reply to Community Help

    Hi..

      there is query.. if i am swapping image which are on different frames but continuous, can it be swipe smoothly as water flow instead of instant change.

     

    Thanks in advance

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 22, 2012 7:46 AM   in reply to Rasmy2012

    There's so much in this thread would you mind opening a new thread and posting the relevant code so we don't need to read through it all? I think all you want to do is animate properties (position, alpha, etc) of images when changing and if so just use a Flash Tween or another more optimized library like TweenLite to handle animating those properties for you. Then you just specify the object you want to affect as well as which properties and values to change to and it will handle the animation (like move or fade in or out smoothly).

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 23, 2012 3:35 AM   in reply to Community Help

    Hi Sinious,

            Actually i tried both link but thing is that in both case images are loaded in 1 frame and my images are loaded in different frame such as 1 image on 1 frame, so i want to ad flick scroll between two frame while i swipe the image.

     

     

    Thanks

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 23, 2012 7:34 AM   in reply to Rasmy2012

    What you're saying is you want to see one exiting at the same time another is entering. So both on the screen at the same time. In that case you'll need to modify your timeline so they both exist at the same time. There's no way around that. It's not like powerpoint where you can choose a transition between frames.

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 24, 2012 8:10 AM   in reply to sinious

    Hi Sinious,

                    I never used timeline before, don't have any idea. If you don't mind plz provide me a sample.

     

     

    Thanks 

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 24, 2012 8:29 AM   in reply to Rasmy2012

    Please open a new thread and give as much detail about your issue as you can. Detail out how you're placing your images (code/timeline/etc), the software you're using including version, what you want to do and any code you're currently trying to get the job done. Thanks.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points