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.
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/
-
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>
-
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);
-
This works in iOS just fine, but not in android…
<html>
<body>
<img src="../images/test.png" />
</body>
</html> -
I also can confirm "../images/test.png" isn't working in android.
-
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});