Jump to content
Search Community

prependURLs with File.userdirectory.nativepath?

Mr Pablo test
Moderator Tag

Recommended Posts

Building a Flex Mobile app for Android.

 

I am using LoaderMax to load assets held on local disk (not a remote server).

 

In my XML file, I have the file names as the URL, but I need to use the prependURLs parameter to pass in the File.userDirectory.nativepath (which on Windows, points to C:\Users\Username and on Android, points to the sd card)

 

Even though File.userDirectory.nativePath returns a string, it breaks the loader if i use it as the prependURLs parameter.

 

How can I point the Loader to the correct directory? Currently, my assets to be loaded have to sit inside the applications "src" folder - not ideal or practical for my actual needs!

Link to comment
Share on other sites

Let's take prependURLs out of the equation for now.

 

Can you get something like this to work

 

 

 

var path:String = File.userDirectory.nativePath;

var image:ImageLoader =  new ImageLoader(path + "someFilename.jpg", {container:this})
image.load()
 

 

In other words are you positive that you are building the correct path to the asset and that ImageLoader has permission and the capability to load it from that location?

 

Quite frankly, LoaderMax (and all its loaders) were built with the purpose of loading and tracking the progress of remote assets. We're aren't terribly familiar with all the subtle nuisances of working with the file systems on all these new mobile platforms. We have heard of people having varying levels of success with mobile apps. 

 

http://forums.greensock.com/topic/2688-loadermax-imageloader-clarrification/?hl=nativepath#entry6349

 

Perhaps something in that post triggers some ideas.

 

For a situation like this, it just makes sense to make sure all the info you are passing in is correct before trying to figure out if there is something that needs to be or even can be fixed with prependURLs. 

Link to comment
Share on other sites

G'day mate,
 
When your working with AIR I think you have to resolve the path in order to get the location as iOS and Android both store they files in differection locations. Hence its a function and not a static variable. Here's a snippit from something I'm working on, maybe it will help you:

 

//thumbImage = APP_SETTINGS.OVERLAY_THUMB_PATH + APP_GLOBALS.OUTDOOR_SELECTED_FOLDER + "/" + filename;

CONFIG::AIR
{

    var imageAIR:File = File.applicationDirectory.resolvePath(thumbImage);
    thumbImage = imageAIR.url;
    //MonsterDebugger.trace(this, thumbImage, "", "From AIR");
}
var ilThumbOverlay:ImageLoader = new ImageLoader(thumbImage, new ImageLoaderVars()
	//.name(name + count)
	.container(vmItem.itemHolder)
    .width(60)
    .height(45)
    .crop(true)
    .scaleMode("proportionalOutside")
)
  • Like 1
Link to comment
Share on other sites

Zync - File.userDirectory. native path returns a string, I can trace it out to console.

 

Carl - I am able to use nativePath in my Flash version of this project where I am writing out the Loaders myself. I'll try and load a single asset with it and see what happens. Surely LoaderMax should be able to cater for this?

Link to comment
Share on other sites

File.userDirectory does have a url property! It was tracing out:

 

 

file:///C:/Users/MyUserName

 

But assigning that to a variable and passing it to prependURLs in the XMLLoader froze the application!?

 

EDIT - running it via Debug doesn't show me the problem, but the Profiler does. I get this error:

 

 

Error #1009: Cannot access a property or method of a null object reference.

 

Just to add to this, when I run it via Debug, I don;t even get the errorHandler messages being traced, it simply locks up before hitting those. Clearly it doesn't like the string being passed?

Link to comment
Share on other sites


Global.storagePath = File.userDirectory.url;

            

private var path:String = Global.storagePath;

            

private function onCreation(e:Event):void

{

    LoaderMax.contentDisplayClass = FlexContentDisplay;

    LoaderMax.activate([ImageLoader, SWFLoader, VideoLoader]);

                

    xmlLoad = new XMLLoader("xml.xml", {name:"xmlDoc", onComplete:completeHandler, onError:errorHandler, prependURLs:path});

    xmlLoad.load();

}

Link to comment
Share on other sites

I still don't know that you answered the question about a single ImageLoader working.

The reason I ask again about the single loader is that it would verify that you are building a valid path that LoaderMax can use. If a single ImageLoader can't get to an image there is no point trying to figure out why prepend urls seems to be breaking things. 

 

Another thought, do the urls in your xml have a leading forward slash?

 

url="/winter.jpg"

 

or does global.StoragePath end with a forward slash?

 

Just want to make sure you aren't prepending something like 

 

"file:///C:/Users/MyUserName" to something like "winter.jpg" which would give you:

 

file:///C:/Users/MyUserNamewinter.jpg

Link to comment
Share on other sites

  • 2 weeks later...

Ok,I was able to use:

private function onCreation(e:Event):void
            {
                LoaderMax.contentDisplayClass = FlexContentDisplay;
                LoaderMax.activate([ImageLoader]);
                
                var path:String = File.userDirectory.nativePath;
                
                var image:ImageLoader =  new ImageLoader(path + "/" + "IMG_4684.JPG", {container:this})
                image.load()
            }
 

And the image loaded. Note I did have to insert the forward slash, but even so, thats a simple thing to correct at any point.

 

I'm thinking it may because I was assigning the userDirectory to a variable outside of the creation function?

 

However, I was testing again with my XMLLoader, putting a variable assigned to the file path as the prependURLs option, the application simply freezes with no error in the console.

 

Why would imageloader work (even if the path is wrong, eg take away the additional slash, it shows a loadermax error) and xmlloader won't?

 

Very odd!

 

EDIT - I am able to use the following URL inside my XML document:

 

file:///C:/Users/MyUserName/FolderName/SwfFile.swf

 

And this loaded fine.

 

So there is clearly an issue with prependURLs as it does not work when I set prependURLS to "file:///C:/Users/MyUserName/FolderName/" !!

 

Is there a way to prepend/modify the URLs in the XML without the loader option?

Link to comment
Share on other sites

Are you absolutely positive that you're passing the data in correctly? I noticed that previously you used File.userDirectory.url and then when you said you moved the variable declaration into the function, you were actually using File.userDirectory.nativePath. 

 

Also, you mentioned an error you saw in the profiler about a null object - can you provide more information? Exactly what line was encountering that error? Maybe if it points to a line inside XMLLoader, it would help identify the issue. 

 

I assume you're using the latest version of LoaderMax/XMLLoader, right?

 

And to answer your last question, yes, you could add an onRawLoad listener to the XMLLoader which will get called as soon as the XML has loaded but BEFORE it gets parsed by XMLLoader (looking for LoaderMax-related nodes). That way, you could do whatever you want to the XML, changing paths, etc. In other words, DON'T use prependURLs but instead prepend the URLs yourself manually as soon as the XML loads. You don't even need to feed things back into XMLLoader at that point because the XML object itself is referenced inside XMLLoader, so if you edit that object (its nodes), it will automatically flow through to XMLLoader when it looks for LoaderMax-related nodes. 

Link to comment
Share on other sites

I actaully tried both url and nativePath, as well as userDirectory and documentsDirectory.

 

Neither combinations worked with prependURLs.

 

I got my rawLoad Handler working to edit the URL in the XML and it works perfectly.

 

I have another issue now which I posted in a new thread :) The fun never ends haha!

Link to comment
Share on other sites

Glad you got it working. I'm really curious, though, about the prependURLs thing - I strongly suspect you were passing the data in incorrectly, but maybe there is indeed a bug somewhere. Can you do me a favor? Please do the prependURLs thing the way you were but DON'T set any of your loaders to load (remove any load="true" in the XML). Then, when the XMLLoader loads, do this:

 

 

var loaders:Array = LoaderMax.getLoader(yourXMLLoader.name + "_ParsedLoaders").getChildren();
for (var i:int = 0; i < loaders.length; i++) {
    trace(loaders[i].url);
}
 

 

And see if the URLs are what you expected. Obviously replace "yourXMLLoader" with a reference to your XMLLoader instance. 

Link to comment
Share on other sites

No worries. Yeah, you'd put it in the onComplete handler (or even in an onInit would be fine - either way). And no, don't remove the load() command. The whole point is to have it load the XML and apply the prependURLs and then we're tracing out the resulting URLs for each of the loaders so that you can see if there are any typos in your paths (like a missing "/").

Link to comment
Share on other sites

public function completeHandler(e:LoaderEvent):void
            {
                trace("assets loaded");
                var loaders:Array = LoaderMax.getLoader(xmlLoad.name + "_ParsedLoaders").getChildren();
                for (var i:int = 0; i < loaders.length; i++) {
                    trace(loaders[i].url);
                }
            }
 

gave the following error:

 

 

ReferenceError: Error #1069: Property url not found on com.greensock.loading.LoaderMax and there is no default value.

 

:/

Link to comment
Share on other sites

Ah, you must have some LoaderMax nodes in there. Okay, just add some conditional logic. First, add this function:

 

function traceChildren(queue:LoaderMax):void {
    var loaders:Array = queue.getChildren();
    for (var i:int = 0; i < loaders.length; i++) {
        if (loaders[i] is LoaderMax) {
            traceChildren(loaders[i]);
        } else {
            trace(loaders[i].url);
        }
    }
}

 

And then, in your onComplete handler, add this:

 

traceChildren( LoaderMax.getLoader(yourXMLLoader.name + "_ParsedLoaders") );

 

Does that work?

Link to comment
Share on other sites

I used your traceChildren function and I got the correct file locations (I had to apprend the path with  "\\foldername\\")

 

If i copy and paste the console output into Windows Explorer, it takes me to the correct file.

 

But If i set load="true" in my XML, it still breaks.

 

Definitely a bug somewhere in LoaderMax?

Link to comment
Share on other sites

Actually, I'd say that points in the opposite direction - that there isn't a bug in LoaderMax. The point of that exercise was to verify whether or not XMLLoader prepended the URLs correctly and from what you're saying, it did. 

 

So if you define the paths directly inside the XML (not using prependURLs at all), things work fine? And did you use prepend on that EXACT same scenario and it broke? Or did you just try the prependURLs thing in a totally different scenario? Again, the goal is to find out if the resulting URLs are truly identical or not (using prependURLs vs. defining them directly in the XML).

 

It's kinda tough to troubleshoot on this end because you're the only one who has the real code and can publish and see the issues. Is there any way you can create a SUPER-simple FLA that demonstrates the problem so that you can send us that FLA and an XML file (and whatever file(s) you're loading) so that we can publish on our end and see the issue in context? 

Link to comment
Share on other sites

Can you provide more details? I asked a few questions that I don't think I got answers to yet...

  1. So if you define the paths directly inside the XML (not using prependURLs at all), things work fine? And did you use prepend on that EXACT same scenario and it broke? Or did you just try the prependURLs thing in a totally different scenario? Again, the goal is to find out if the resulting URLs are truly identical or not (using prependURLs vs. defining them directly in the XML).
  2. Is there any way you can create a SUPER-simple FLA that demonstrates the problem so that you can send us that FLA and an XML file (and whatever file(s) you're loading) so that we can publish on our end and see the issue in context?

You said it broke when you set load="true" - are you talking about when you were using prependURLs or when you were defining the path directly in the XML? Or both? Please make sure you're trying BOTH scenarios and let us know how they compare.

Link to comment
Share on other sites

Can you provide more details? I asked a few questions that I don't think I got answers to yet...

  1. So if you define the paths directly inside the XML (not using prependURLs at all), things work fine? And did you use prepend on that EXACT same scenario and it broke? Or did you just try the prependURLs thing in a totally different scenario? Again, the goal is to find out if the resulting URLs are truly identical or not (using prependURLs vs. defining them directly in the XML).
  2. Is there any way you can create a SUPER-simple FLA that demonstrates the problem so that you can send us that FLA and an XML file (and whatever file(s) you're loading) so that we can publish on our end and see the issue in context?

You said it broke when you set load="true" - are you talking about when you were using prependURLs or when you were defining the path directly in the XML? Or both? Please make sure you're trying BOTH scenarios and let us know how they compare.

 

Apologies, was rushed for time earlier.

 

If I define the file path in the XML document, it works.

 

I used prependURLs in the same scenario and it failed.

 

When I set load to true AND use prependURLs, thats when it broke..

 

I have converted the project to Flex, but the AS3 code is what matters. I will try and sort out some files to upload for you.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...