Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Contributor
twilightdema
Posts: 17
Registered: ‎02-10-2011
My Device: Not Specified
Accepted Solution

Problem about writing file in Documents shared directory.

Hi,

 

     I'm new in PlayBook development. I'm trying to create a application that can save image file inside shared documents directory (so other image viewer application can find it). Below is my code:

 

                var file:File = File.documentsDirectory;    
                /////////////////////////
                // Try writing a file...
                var jpgEncoder:JPEGEncoder = new JPEGEncoder();
                var jpgData:ByteArray = jpgEncoder.encode(bmp);
                file.addEventListener(Event.COMPLETE, onFile_SaveComplete);
                file.save(jpgData, filename);

 

I got a file saving dialog when I call "file.save(...)". However, after I type in file name to be saved and press "OK" button, I got the exception as below:

 

Error #2044: Unhandle IOErrorEvent:. text=Error #2038: File I/O Error.

       at flash.filesystem::File$/getFile()

       at flash.filesystem::File$/get_documentsDirectory()

        .....

        .....

 

The same code run perfectly on my desktop, thus I wonder if there're some other restriction in PlayBook OS to prevent me from saving file in shared document directory? Such as, do I have to sign my application with certificate?

 

[ I've read document and understand that "File.documentsDirectory" should be writtable... May I I'm misunderstanding..? ]

 

Best Rergards,

Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Problem about writing file in Documents shared directory.

hey,

 

welcome to the forums! this has just been discussed in a previous thread from yesterday. basically the file browsing dialogs are unfinished and result in weird erros whenever you try using a method that requires a file browse dialog. the work around right now is to use the FileStream class to write your files to the hard disk of the playbook until all the dialogs are fixed on the platform.

 

here is the link to the previous thread. in it you will find the work around code that you can adapt to your needs. also there is a little more explanation:

 

http://supportforums.blackberry.com/t5/Tablet-OS-SDK-for-Adobe-AIR/Saving-Syntax-Incorrect/m-p/78610...

 

hope that helps. good luck!

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10

Re: Problem about writing file in Documents shared directory.

Add the event handler for IOErrorEvent along side of the Event.COMPLETE.  Have it do nothing.

 

file.addEventListener( IOErrorEvent.IO_ERROR, DoFailed );


...


private function DoFailed( event : IOErrorEvent ) : void

{

  // do nothing 

 

This probably has nothing to do with signing.  BB has not told us how and when signing will occur.

Contributor
twilightdema
Posts: 17
Registered: ‎02-10-2011
My Device: Not Specified

Re: Problem about writing file in Documents shared directory.

Hi JRab,

 

       Thank you very much for your answer. It's really get to the point!    :smileyhappy:

 

Contributor
twilightdema
Posts: 17
Registered: ‎02-10-2011
My Device: Not Specified

Re: Problem about writing file in Documents shared directory.

Just want to share that I ended up writing my own simple File Browser to be used until the problem in Simulator is solved... Below is my code:

 

 

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark" title="FileBrowserView">
    <fx:smileyvery-happy:eclarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:smileyvery-happy:eclarations>
    
    <fx:smileyfrustrated:cript>
        <![CDATA[
            
            import mx.core.UIComponent;
            
            import spark.components.MobileApplication;
            
            private var currentSelectedLabel:Label = null;
            private var currentDir:File;
            
            private var saveDialog:Boolean;
            private var callbackFunction:Function;
            private var extensionFilter:smileyfrustrated:tring;
            
            public function initializeAndLoad(isSaveFileDialog:Boolean, callback:Function, extension:smileyfrustrated:tring):void
            {
                extensionFilter = extension;
                saveDialog = isSaveFileDialog;
                callbackFunction = callback;
                currentSelectedLabel = null;
                currentDir = File.documentsDirectory;
                updateCurrentDirectory();
            }
            
            protected function onClick_FileLink(event:MouseEvent):void
            {
                var fileLabel:Label = event.currentTarget as Label;
                
                var selectedFile:File;
                if(fileLabel.text == "..")
                    selectedFile = currentDir.parent;
                else
                    selectedFile = currentDir.resolvePath(fileLabel.text);
                
                if(selectedFile == null) return;
                if(selectedFile.isDirectory)
                {
                    currentDir = selectedFile;
                    textinput_FileName.text = "";                    
                    updateCurrentDirectory();
                }
                else
                {
                    textinput_FileName.text = fileLabel.text;                    
                    if(currentSelectedLabel != null)
                        currentSelectedLabel.alpha = 0.3;
                    currentSelectedLabel = fileLabel;
                    currentSelectedLabel.alpha = 1.0;
                } // if
            }
            
            protected function updateCurrentDirectory():void
            {
                var dirs:Array = currentDir.getDirectoryListing();
                
                fileList.removeAllElements();
                var fileLabel:Label = null;
                
                // Link to parent dir
                if(currentDir.parent != null)
                {
                    fileLabel = new Label();
                    fileLabel.width = fileList.width;
                    fileLabel.alpha = 0.3;
                    fileLabel.text= "..";
                    fileLabel.addEventListener(MouseEvent.CLICK, onClick_FileLink);
                    fileList.addElement(fileLabel);
                    fileLabel.opaqueBackground = 0xff333344;
                } // if
                // Directories
                var i:uint;
                for(i=0;i<dirs.length;i++)
                {
                    if((dirs[i] as File).isDirectory && !(dirs[i] as File).isHidden)
                    {
                        fileLabel = new Label();
                        fileLabel.width = fileList.width;
                        fileLabel.alpha = 0.3;
                        fileLabel.text = (dirs[i] as File).name;
                        fileLabel.addEventListener(MouseEvent.CLICK, onClick_FileLink);
                        fileList.addElement(fileLabel);
                        if(fileList.numElements % 2 == 0)
                            fileLabel.opaqueBackground = 0xff222233;
                        else
                            fileLabel.opaqueBackground = 0xff333344;
                        trace("Dirs ", (dirs[i] as File).name);
                    } // if
                } // for                                
                // Files
                for(i=0;i<dirs.length;i++)
                {
                    if(!(dirs[i] as File).isDirectory && !(dirs[i] as File).isHidden)
                    {
                        if(!saveDialog && (dirs[i] as File).extension.toLocaleLowerCase() != extensionFilter.toLocaleLowerCase())
                            continue;
                        fileLabel = new Label();
                        fileLabel.width = fileList.width;
                        fileLabel.alpha = 0.3;
                        fileLabel.text = (dirs[i] as File).name;
                        fileLabel.addEventListener(MouseEvent.CLICK, onClick_FileLink);
                        fileList.addElement(fileLabel);
                        if(fileList.numElements % 2 == 0)
                            fileLabel.opaqueBackground = 0xff222222;
                        else
                            fileLabel.opaqueBackground = 0xff333333;
                        trace("Files ", (dirs[i] as File).name);
                    } // if
                } // for                                
            }

            
            protected function onClick_OK(event:MouseEvent):void
            {
                var f:File;
                if(saveDialog)
                {
                    // Save file dialog    
                    f = currentDir.resolvePath(textinput_FileName.text);
                    if(f.exists && f.isDirectory)
                    {
                        currentDir = f;
                        textinput_FileName.text = "";                    
                        updateCurrentDirectory();                        
                    }
                    else
                    {
                        // Append extension if need.
                        if(
                            (textinput_FileName.text.length <= extensionFilter.length + 1) ||
                            (textinput_FileName.text.substr(
                                textinput_FileName.text.length-extensionFilter.length-1,
                                extensionFilter.length+1).toLocaleLowerCase() != ("."+extensionFilter).toLocaleLowerCase()))
                        {
                            textinput_FileName.text += ("."+extensionFilter)
                        } // if
                        f = currentDir.resolvePath(textinput_FileName.text);
                        if(f.isDirectory) return;
                        else if(f.exists)
                        {
                            callbackFunction(f);                        
                        }
                        else
                        {
                            callbackFunction(f);                
                        } // if
                    } // if
                }
                else
                {
                    // Open file dialog    
                    f = currentDir.resolvePath(textinput_FileName.text);
                    if(f.exists && f.isDirectory)
                    {
                        currentDir = f;
                        textinput_FileName.text = "";                    
                        updateCurrentDirectory();                        
                    }
                    else
                    {
                        if(f.exists)
                        {
                            callbackFunction(f);                        
                        }
                        else
                        {
                            return;
                        } // if
                    } // if
                    
                } // if
            }

            protected function onClick_Cancel(event:MouseEvent):void
            {
                callbackFunction(null);
            }
            
        ]]>
    </fx:smileyfrustrated:cript>

    <s:VGroup x="10" y="10" width="460" height="378">
        <s:HGroup width="459" height="93" verticalAlign="middle">
            <s:Label text="File Name: "/>
            <s:TextInput id="textinput_FileName" width="332"/>
        </s:HGroup>
        <s:smileyfrustrated:croller width="460" height="170">
            <s:VGroup width="450" height="160" id="fileList">
            </s:VGroup>
        </s:smileyfrustrated:croller>
        <s:HGroup width="458" height="91" verticalAlign="middle">
            <s:Button id="button_OK" label="OK" click="onClick_OK(event)"/>
            <s:Button id="button_Cancel" label="Cancel" click="onClick_Cancel(event)"/>
        </s:HGroup>
    </s:VGroup>
</s:View>

Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Problem about writing file in Documents shared directory.

hey twilightdema,

 

thanks for the follow up post and sharing your code with us. im sure it will come in handy for a lot of people!

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman