Android - Show Local Images in WebView [duplicate] - java

This question already has answers here:
Android - local image in webview
(11 answers)
Closed 6 years ago.
I found out that in earlier APIs of android (KitKat and abover), local images (for example pictures in assest folder) can't be load in WebView! I have a html file that contains tag to show images.
<img src="blacksmoke1.jpg">
And I put blacksmoke1.jpg in assest folder. but nothing shown in WebView.
This is the problem: https://code.google.com/p/android/issues/detail?id=63033
How can I fix it? Is there an alternative way to show pictures in webview? Or Is there a custom WebView that i can implement in my app?
Edit:
This my assest folder:

Try creating a web pages in your assets directory and creating HTML pages that display the images. Then call a web page using this:
webview.loadUrl("file:///android_asset/blacksmoke.html");
Or, according to one of the posts in your link, move the html file to a server and use this code:
loadDataWithBaseUrl("content://<your contentProvider>/blacksmoke.html", ...);
Code in blacksmoke.html could be:
<!DOCTYPE html>
<!-- Perhaps some JQuery mobile for more functionality and control over which images get displayed and why etc -->
<body>
<div><img src="image/blacksmoke1.jpg"></div>
</body>
</html>
Here is a good explanation for creating native pages or displaying native images.

This is what I do to load local resources in webview from assets folder:
StringBuilder sb = new StringBuilder();
if (notification.get_id().equals("53be76c3d6eac5f54a546176")){
sb.append("<html xmlns=\"http://www.w3.org/1999/xhtml\"> <head> <title>For.... <img src=\"data1/images/forum1.jpg\" alt=\"\" title=\"\" id=\"wows1_0\"/> </body> </html>");
That is the html i want to load, if you notice there is an img tag, loading an image from data1 folder which is placed in assets folder. In order to be able to load resources from there I do the following:
WebView webView = (WebView) findViewById(R.id.webview);
webView.loadDataWithBaseURL("file:///android_asset/", sb.toString(), "text/html", "utf-8", null);
Hope it helps

Related

jsp page dosent load css when using request dispatcher

enter image description hereso i was trying to load a jsp page when the submit button was clicked when i press the submit button data isent to the servlet.so the page loads but without the css.
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/answerPage.jsp?id=" + id);
requestDispatcher.forward(request, response);
this didnt work so i tried this.
response.sendRedirect("answerpage.jsp");
it just gave me the 404 error
Without seeing the code to your answerPage.jsp, I would ensure that you have the css page linked in the html, something like:
<link rel="stylesheet" type="text/css" href="styles.css">
And just make sure that the css file is located in the WEB-INF folder in your project
so i found the answer to my problem.the problem was in the url mapping of my xml file

Wicket Bookmarkable link

I have created a new link in the Welcome.java as the following
private Link<Void> drawLink;
drawLink = new BookmarkablePageLink<Void>("drawSome", drawSomething.class);
add(drawLink);
The following goes in the Webpage.html
<li>
Draw
</li>
There are two issues I want to fix.
I get the url as
localhost/project-name/wicket/bookmarkable/package-name.drawSomething?0
but I wanted the link to be as
localhost/project-name/drawSomething?0
As the drawSomething is a new page added to the project, like welcome page I am having a drawSomethign.properties file for page.icon and page.title.
page.title=D3 vis
page.icon=images/home_page.png
The drawSomething page loads the page title but throws an error for the image as could not resolve images folder.
But I have images folder in src/main/webapp/images/home_page.png
Can anyone please help me resolve both the issues.
Do this during you application initailization:
#Override
protected void initialize() {
mountPage("drawSomething", drawSomething.class);
}
Path to the images folder is relative to webroot path, try ../images/home_page.png

Get the main content from RSS Feeds

I've created an application which lists all the feed items in ListFragment. My question is how to load the full text from the link. I've tried with WebView, but it is loading the complete URL. Since RSS XML structure does not have any tags related to full/actual content, can anyone give some reference so that I can use it?
This is how I've tried to load the URL in WebView:
URL = getIntent().getStringExtra("url");
webView = (WebView) findViewById(R.id.activity_display_news_webview);
webView.loadUrl(URL);
You can use boilerpipe to extract the full text from the url

display a photo on jsp file using a url

I'm trying to display a photo on my jsp file. a photo that is inside my images photos works fine but when i'm trying to retrieve a photo using a url from the internet it doesnt work.
my url is a link of a database domain.I'm saving my photos using blobstore in that domain and retrieve them later.
I know the link works fine because when I use it in my explorer I receive the photo I uploded so what could be wrong? or is there a better way to display photos on a jsp file using blobStore ?
I'v search the web for something similar but all the solutions are for photos save in the program or not displaying on jsp.
my code in the jsp file is :
<%String blob=request.getParameter("blob");
BlobKey blobKey = new BlobKey(blob);
ImagesService services = ImagesServiceFactory.getImagesService();
ServingUrlOptions serve = ServingUrlOptions.Builder.withBlobKey(blobKey);
String url = services.getServingUrl(serve);
%>
<center><img src="url" alt='photo'/></center>
I've tried printing the url I received to make sure it is the right path and it works fine when I put it in my explorer so what could be the problem ? thanks for your help
Syntax error.You cannot access variable like that. Have you tried the following?
<img src="<%= url %>" />

LWUIT HtmlComponent render local image

I want to render content that i have created locally using html component and put image in this html also by putting image in res folder in jar, i tried
<img src='images/down.png'></img>
<img src='res/images/down.png'></img>
<img src='./images/down.png'></img>
but nothing worked, any suggestion?
[EDIT]
here is my code, i have no idea how to implement DocumentRequestHandler that is why i used DefaultDocumentRequestHandler
DocumentRequestHandler handler = new DefaultDocumentRequestHandler();
HTMLComponent component = new HTMLComponent(handler);
component.getStyle().setBorder(Border.createLineBorder(1));
component.getSelectedStyle().setBorder(Border.createLineBorder(1));
component.setBodyText("<div><b>nirmal:</b>" +
"<img src='res://images/down.png' /></div>");
tried res://images/down.png but nor worked
my image is in res/images
You need to explain how you loaded the HTML, images are loaded relatively to the base URL so you need to define the base URL when creating the HTML (its implicitly detected when loading via URL).
If you created the HTML via setHTML(String) then you need to give absolute paths depending on your DocumentRequestHandler implementation e.g. res://myImage.png or file://myImage.png .

Categories