WebView how to get local res\drawable-hdpi image path - android

I have a WebView and I want to add a backgroung Image
String htmlPage = "<HTML><BODY background=\"bgrnd.png\" TEXT=\"black\">";
the problem is how to get the image path "bgrnd.png" which is saved in res"\drawable-hdpi" folder.
Thx.

Put your images into assets folder and you can use like this way into your image tag
<img src="file:///android_asset/images/yourimage.png">

In xml use
android:background="#+drawable/bgrnd.png"
Set
mWebView.setBackgroundColor(0); below your string

Related

Android: load image from local storage to webview

I download the image and store in the local storage. I load this image from the html file from local storage.
<img src="file:///data/data/com.example/imagefiles/photo.jpg"/>
I load the html file from WebView.
webView.loadUrl("file:///android_asset/show_download_image.html");
But the image didn't shown. I am sure the downloading image is successful.
I want to know that is it possible to load the image from local storage into the html file or is there any way to load the image from local?
Thanks.
Why not try to load image directly like this:
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
String imagePath = "file://"+ base + "/photo.jpg";
String html = "<html><head></head><body><img src=\""+ imagePath + "\"></body></html>";
webView.loadDataWithBaseURL("", html, "text/html","utf-8", "");
Here the image is in sd card. You can change the code for keeping image in asset also.
check your webview javaScriptEnabled is true?
WebView.getSettings().setJavaScriptEnabled(true);

How to load local html pages to android

I want to load local HTML pages in to a WebView. I know how to do it in iPhone. Can any one tell me the way to do it in android.
How to do the below page in android?
put your file to asset folder than you can use the code below
WebView webview = (WebView) findViewById(R.id.yourwebview);
webview .loadUrl("file:///android_asset/UI/mylocalhtmlfile.htm");
use this.
yourWebView.loadUrl("file:///android_asset/yourfileName.html");
Create a TextView textArea and set it's text with the html string
You can define the string as a part of strings.xml file with your html tags and make sure you call getText(T.string.your_html_string) to fetch the html based text
<string name="your_html_text">"Press <font fgcolor="#ffffffff"><b>Menu</b></font> and touch:\n
\n<li><font fgcolor="#ffffffff"><b>Create </b></font> to create\n</li>
\n<li><font fgcolor="#ffffffff"><b>Delete All</b></font> to delete all\n</li>
</string>
and setText like
textArea.setText(getText(R.string.your_html_text));
The easiest way would probably be to put your web resources into the assets folder then call
webView.loadUrl("file:///android_asset/filename.html");

PhoneGap show image downloaded with $.get

I'm trying to inject HTML code downloaded from URL into my page.
Here the JQuery code
$('#messageScreen').live('pageshow', function() {
$('#messageContent').empty();
var url = testServerDirAdrs + 'message?message_id=' + selectedMessageId;
$.get(url,
function(data){
$('#messageContent').append(data).trigger('create');
},'html');
});
It works fine, but when downloaded html contains images, they are not displayed.
The image tag downloaded from the URL is:
<img src="/images/logos/muni_1_small.png" />
I guess the problem is with the path.
Any idea how can I solve this problem?
Thanks, Costa.
Below you can find 2 possible solutions:
The image can be downloaded from the web so the src could be like src="http://www.example.com/example/myexampleimg.png"
The image exists locally and the img tag's src must have relative path. For example if the html file in which the img tag will be appended is in the same path as your images folder then the path will be ./images/myexampleimg.png. In case your images path is one folder before then the path will be ../images/myexampleimg.png etc
I hope this helps.

Path of image in html file when i load on android webview

I have some html content which has to be displayed on android webview. Here is my code to display the content on webview
myBrowser.loadDataWithBaseURL("", myFormHTMLContent , "text/html", "UTF-8", null);
I have few img tags in html content. How can i specify the src for those? I tried this .. src = "/mnt/sdcard/myDir/test.gif". But its not loading that image. Please help me.
Try this.
src = Environment.getExternalStorageDirectory().getPath()+"myDir/test.gif"

How to load image file with space in name in android web view?

I have problem in loading image resource file in Android webview. Image name has space in words e.g,
file:///android_asset/images/abc xyz.jpg
So problem is webview consider %20 in name when ever space comes so it can not compare with image which is inside asset folder.
I am not sure is there any problem with asset manager which is giving name like just abc I mean before space name or its problem in webview?.
file:///android_asset/images/abc.html
create abc.html save this abc.html to your assets folder
...
<body>
<img src="file:///android_asset/images/abc xyz.jpg"/>
</body>
....
String url = "file:///android_asset/images/abc.html"

Categories

Resources