Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Local Images and Webviews

Can we get a straight answer from an Appcelerator Developer.

There's half a dozen questions asking similar yet different things about webviews and local resources.

  1. Can remote urls access local resources or is this disallowed

  2. If Remote is Allowed what is the local url to call the resource

  3. If Local, is it different to the Remote.

  4. Can someone give a straight description of the file structure plus a valid img or script src call.

  5. does this also work for CSS files reference by the html

EXAMPLE
So you have html from website either downloaded directly or via XHR then loaded.
I have a folder "Resources/Images/File.png", Do i Call the image by…

  1. src="Images/File.png"

  2. src="app://Images/File.png"

  3. src="app://Resources/Images/File.png"

  4. or something else entirely.

In this example i have used all of the above,
from a remote url AND by downloading a temp.html into the appdirectory with the following code

var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'test.html');
f.write(this.responseText);
webview.url = f.nativePath;

code taken from Line 68

Is this correct ?

— asked May 31st 2010 by Ryan Tregea
  • android
  • httpclient
  • local
  • resources
  • webview
  • xhr
0 Comments

3 Answers

  • I suspect it's not allowed - it would be a security risk for remote web pages to be able to access local files. I don't know why you'd want that anyway. If you must have it, download the remote webpage to a local file then load that in your webview. There are plenty of examples in the KitchenSink.

    — answered May 31st 2010 by Damien Elmes
    permalink
    1 Comment
    • This seems like the best solution anyway. But I have another question that I keep asking with no response. Is it possible to synch a directory from the web to local dir. Perhaps at app startup? I can think of ways to write code to do this, but I can not image tha thtere is not already a solution written to do this. Perhaps creating a manifeast file?

      — commented June 9th 2010 by vincent youmans
  • As Damien pointed out, lots of examples in Kitchen Sink that demonstrate this.

    Remote webviews can't access local APIs - with the exception of Ti.App.fireEvent, Ti.App.addEventListener, Ti.App.removeEventListener, Ti.API.debug, Ti.API.info, Ti.API.error, etc.

    You can fire events from your remote webview and then listen for them (using Ti.App.fireEvent, etc) from the "outside" … i.e. in app.js or the window that created the webview.

    The creator of the webview can also evalJS against the webview instance as well to send/retrieve values directly.

    — answered May 31st 2010 by Jeff Haynie
    permalink
    0 Comments
  • I've done some digging, and found that this is where you can find files from your app, Note the triple /// it's important
    "file:///android_asset/Resources/"
    It's still not available to remote sources, but at least it's something

    — answered June 8th 2010 by Ryan Tregea
    permalink
    2 Comments
    • You should use app:// instead of file:///android_asset/Resources/

      — commented June 8th 2010 by Don Thorp
    • See, i couldn't get app:// working, Not sure what i was doing wrong exactly, but yeah, in the end I have working apps on this.

      — commented June 9th 2010 by Ryan Tregea
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.