Titanium Community Questions & Answer Archive

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

Load local image in a webview?

I am using webviews with HTML to display sets of paragraphs. I have a few pages where I want to show paragraphs and images in the html file.
<img src="app://Resources/Images/File.png" alt="File" />

Is there a better way to do this? I am using html because I have text that is highly formatted. I can copy it from the RTF into the HTML file dreamweaver, and all of the formatting is kept.

— asked February 6th 2012 by mike zal
  • html
  • webview
  • webview, html
0 Comments

6 Answers

  • umm that's the best way.

    If that is not working , you could move everything you want to Titanium.Filesystem.applicationDataDirectory from the application , and from JavaScript , say to WebView that you want all images to be Titanium.Filesystem.applicationDataDirectory/relativepathblabalbla/

    — answered February 6th 2012 by Filip Dimitrovski
    permalink
    1 Comment
    • Even though i'm not sure on what OS you are working on. Maybe iOS doesn't accept local including with absolute path

      — commented February 6th 2012 by Filip Dimitrovski
  • Make sure your img.src is correct.

    Here's a working sample structure:

    Resources
        -> /html
            -> /index.html
        -> /images
            -> /test.png
    
    // app.js
    var w = Ti.UI.createWindow();
    
    var wv = Ti.UI.createWebView({url:'html/index.html'});
    w.add(wv);    
    w.open();
    
    //index.html
    <html>
    <body>
        <img src="../images/test.png" />
    </body>
    </html>
    
    — answered February 7th 2012 by Minh Nguyen
    permalink
    0 Comments
  • hi ,
    Look at this code , will work for you : for this place image in resource/images folder and directly give image name in img_pth variable.

    var webview = Ti.UI.createWebView({

    borderRadius : 10,

    width : 1030,

    height : 600,

    top : 0,

    backgroundColor:'#00000000',

    scalesPageToFit:true,

    touchEnabled: true,

    });

    wdt = 100;

    hgt = 200;

    img_pth = 'viewbackground.png'; // Insert your image url.

    webview.html = '<html><body> <img src="images/'+img_pth+'" width='+wdt+' height='+hgt+'></body></html>';

    win.add(webview);

    — answered February 7th 2012 by Moiz Chhatriwala
    permalink
    0 Comments
  • This works in iOS just fine, but not in android…

    <html>
    <body>
    <img src="../images/test.png" />
    </body>
    </html>

    — answered February 7th 2012 by mike zal
    permalink
    3 Comments
    • This also work fine in android as well , just use this properly,

      For more help please share code if possible.

      — commented February 8th 2012 by Moiz Chhatriwala
    • android isn't working also for me, I post my code under.

      — commented March 24th 2015 by Andrea Taggiasco
    • just check are you providing proper image path to the src or not.

      — commented March 24th 2015 by Moiz Chhatriwala
  • I also can confirm "../images/test.png" isn't working in android.

    — answered February 21st 2012 by Jonathan O'Brien
    permalink
    0 Comments
  • Android isn't working also for me:

    
    if (Titanium.Platform.name == 'android' && Titanium.Filesystem.isExternalStoragePresent()) {
        // Android with SD
        var filename = Titanium.Filesystem.externalStorageDirectory + "/" + e.row.file + ".jpg";
    } else {
        // iOS and w/o SD
        var filename = Titanium.Filesystem.applicationDataDirectory + "/" + e.row.file + ".jpg";
    }
    
    var winf = Ti.UI.createWindow({
        backgroundColor: "white",
    });
    
    var imgview = Ti.UI.createWebView({width:'100%', top: "60dp", bottom: "0dp", scalesPageToFit:true});
    
    imgview.setHtml('<html><body><img src="'+filename+'" /></body></html>');
    
    winf.add(imgview);
    winf.open({modal:true});
    
    — answered March 24th 2015 by Andrea Taggiasco
    permalink
    0 Comments
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.