0

I am using XE7 Rad Studio to build "apps" for Android and IPhone. Focusing on Android for the moment.

According to the requirements, I need to load the HTML inside the application as a resource string.

WebBrowser1.LoadFromStrings(ResourceStrings.HTMLString,'');
//Loads the resource-string successfully.

However in this resource-string I need to load images, and I cant figure out how to do it. I can see in deployment that I have the images loaded into the project {Bitmap_1, Bitmap_2,Bitmap_3}.

How do I complete this line:

resource-string:

...'<a href="/images/im2.bmp"><img id="img2" class="thumbnail" src="/images/im2.bmp" alt="/images/im2.bmp"/></a>'...

Many thanks.

Johan
  • 74,508
  • 24
  • 191
  • 319
  • 1
    I may be missing something, so perhaps you could clarify..... The fact that your HTML string refers to image files should be irrelevant at this time. You simply need to extract a string a give it to your WebBrowser component to process as HTML. When your WebBrowswer decides it needs the image, it will try load it as instructed in the HTML. Most likely it will look for a physical file `/images/im2.bmp`. To ensure it finds the file you may need to create/copy it in/to the correct location. If you're going to provide physical image file, then you may as well also provide a physical HTML file. – Disillusioned Nov 17 '14 at 16:58
  • It's not clear to me what you're really asking for. As you've explained it so far, the browser control will issue network requests for the other page resources. The server it sends them to depends on the page's base URL. Is that URL *localhost*, and is your program a Web server? If so, then you need to receive those requests and serve the corresponding resources. If you're not a server, but you still want to provide those images, then you need to send the data to the browser some other way. Consider setting the `href` attributes to [**data URIs**](http://en.wikipedia.org/wiki/Data_URI_scheme). – Rob Kennedy Nov 17 '14 at 17:19
  • @RobKennedy: Does `TWebBrowser` support data URIs? – Remy Lebeau Nov 17 '14 at 22:30
  • @Remy, isn't it just a wrapper for the system browser? The native browser for Android supports data URIs, according to the page i linked to before. – Rob Kennedy Nov 17 '14 at 22:33
  • Thanks for all the comments and thoughts, Your helpfulness shall be payed forward. – Hans Hagman Nov 20 '14 at 06:28
  • @CraigYoung 1. I tried looking at the deployment list, and used that to find files. But I think that the second parameter was at fault. 2. Yes I would also like to use an actual HTML file, however the project lead wanted to protect content. – Hans Hagman Nov 20 '14 at 06:30

1 Answers1

0

If you read the documentation for LoadFromStrings(), it says:

Displays HTML string content within the TWebBrowser component.

This method uses the following parameters:

  • Content: specifies the HTML string to be displayed.

  • BaseUrl: specifies a path that is used to resolve relative URLs within the loaded page. To clarify, consider the following scenario: this parameter is set to www.mycompany.com/departments/, and the loaded page defines a link <a href=’Sales.html’>Sales dept</a>. In the given case, clicking this link opens http:// www.mycompany.com/departments/Sales.html.

That is the exact scenario you are running into. Your HTML contains relative links to external images, but you are not providing a BaseURL, so the WebBrowser cannot resolve the correct URLs it needs to load those images.

In the Deployment Manager, set the Remote Path of your image files to either StartUp/Documents/images/ or StartUp/Library/Application Support/images/. At app startup, Delphi will copy files beginning with StartUp to the appropriate folder on the device. Then you can do the following when calling LoadFromStrings():

// note sure which function to use for 'StartUp/Library/Application Support/',
// maybe TPath.GetLibraryPath()? This example is for '/StartUp/Documents/'...
WebBrowser1.LoadFromStrings(ResourceStrings.HTMLString, 'file://' + TPath.GetDocumentsPath);

That will allow "/images/im2.bmp" to resolve to something like file:///data/data/<application ID>/files/images/im2.bmp", etc.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770