Load image from sdcard into html - android

Hi I have implemented an android web server and I am serving html pages on the fly, based on the uri requested.
When I try to include images from my sdcard they are not showing up on the html page. I have the requisite permissions in the manifest. I have tried looking around but, most of the examples involve images within webview. I am using the following code to get path to the .png files on the sdcard.
String file_icon=Environment.getExternalStorageDirectory().getAbsolutePath()+"/file_icon.png";
String folder_icon=Environment.getExternalStorageDirectory().getAbsolutePath()+"/folder_icon.png";
and I am writing to the outputstream using the following
writer.write("<tr><td><input type=\"checkbox\" name=\""+files.toString()+"\" value=\"file\"/></td><td><img src=\""+file_icon+"\"/>"+files.toString()+"</tr>");
writer.write("<tr><td><input type=\"checkbox\" name=\""+files.toString()+"\" value=\"file\"/></td><td><img src=\""+folder_icon+"\"/>"+files.toString()+"</tr>");
When I fire up the emulator and visit the page I see no images.
screen http://img820.imageshack.us/img820/7973/capturekuw.png
Any ideas or pointers in the right direction would be greatly appreciated.Thanks.

After looking around I found out that I needed to implement a ContentProvider and override the openFile() method.
http://www.techjini.com/blog/2009/01/10/android-tip-1-contentprovider-accessing-local-file-system-from-webview-showing-image-in-webview-using-content/

Related

how to display a pdf file in Web view without storing it in SDCard

I am realizing an android project I am blocked that one I wanted to show a pdf in webview .the pdf file is accessible via an URL in localhost.
when I use this code, webView.loadUrl("https://docs.google.com/gview?embedded=true&url="+pdfurl);
it shows :
"Sorry, we were unable to find the document at the original source. Verify that the document still exists.You can also try to download the original document by clicking here."
Help me to solve this.Thanks in advance.
Your file is located in your local server.It cant accessed from outside your network.Upload your pdf file to any website like this or this.And get the public url from there.Then try to access that url from your application.
You can use the google's document viewer service for viewing the pdf,ppt contents in the webview if your document resource is on online.
create following HTML string like :
String googleDocUrl ="<iframe src='http://docs.google.com/gview?url="
+ YOUR_ONLINE_PDF_DOC_URL
+ "&embedded=true' width='100%' height='100%'style='border: none;'></iframe>";
call webviews loadData() method :
mWebView.loadData(googleDocUrl ,"text/html","UTF-8");
Hope this might help you :)

Images not displaying in Android WebView

I have a situation where I am trying to display user-generated HTML in a webview using loadData. My code is something like this:
String content = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
"<html><head>" +
"<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />" +
"</head><body>";
content += htmlStr + "</body></html>";
wv.loadData(content, "text/html", "UTF-8");
It works pretty well in most cases, but I am having trouble getting certain types of images to display. In most of my testing, inserting <img src="..."/> tags worked fine, but I found that links to images on Photobucket would not display at all; I get a little box with question mark in it instead of the image.
Clicking on a link to an image on Photobucket tends to take you to the page on which that image is viewable on their website, rather than just the raw image itself. I have a feeling that the issue is related to this. I suspect that it may be a "Referer" issue, or perhaps user-agent, or something of that nature, but I cannot for the life of me get this to display properly.
I have tried switching to loadDataWithBaseURL and providing a BaseURL (as I believe this will be used as the referer url) but that made no difference. I have also tried using loadUrl("http://photobucket..." instead and providing a HashMap with the Referer header manually set, but that did not work either. Actually, switching to loadUrl made it immediately redirect to the device's browser to load the Photobucket page. I attempted to provide a custom WebViewClient and override shouldOverrideUrlLoading, but the best I got it to do was to display the full Photobucket page inside the WebView.
I am sure this is not a specific issue with Photobucket, that just happens to be the site that I discovered this problem with while I was testing.
I would really like to figure some way to deal with this situation so that this can work correctly, but I have been as yet unable to find any helpful direction on SO or the internet at large. Does anyone have any ideas?
I would wait until Photobucket finishes their recent updates before further testing...while you stated you are sure it is not a specific issue with Photobucket the internet is now awash with posters not able to view Photobucket linked to images with android based devices. If your code works with all other images not hosted on photobucket your code is good.

Android: Displaying a hyperlink and image inside email's body

I am trying to open email activity and to display in the email's body the following:
Display a hyperlink to a web site.
Display (not attach) an image logo (.png file).
I tried using html/text/image mime type and nothing works for both things.
I even tried to copy the png file to an sdcard and displaying it from sdcard path instead of using the "Assets" location that may be restricted and private only to the application, but it did not help as well!
Can anyone give me a code which works for both things??
Waiting for you help guys!!
Have you tried :
Linkify.addLinks(yourEmailString, Linkify.WEB_URLS);
yourTextView.setMovementMethod(LinkMovementMethod.getInstance());
It will make all links in the message clickable, not sure if this is what you are searching though :)?

Android ImageViewer class supporting Pinch-Zoom and Scrolling

I am searching an ImageViewer library that opens an image in my application for a given URI (the image fetched by a webservice is already stored within my application in a secured place). I really like the "Samsung Galaxy S" ImageViewer-Activity because it uses pinch-zoom and "scrolling" vertical/horizontal. Also it scales the picture very fast on my samsung phone :)
I know that I can open an image with an intent like this:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(uri, "image/*");
startActivity(i);
The best suitable viewer is being called, when none is found an ActivityNotFoundException is raised. So thats cool!
But the problem is that I am not allowed to open an image with an external intent (for security purposes). i.e: The user should not have the posibility to save the opened image via a menu option to his external sd-card or send this picture to another service (email/twitter or s.o.). So I have to write my own ImageViewer-Class (Activity) that can only be called within my application...
Unfortunately I am not very skilled transforming images, so is there any open source project (or library) that covers this use case?
I already asked google and found this one http://code.google.com/p/android-pinch/ but it didnt work very well (also it has no scroll-functionality).
Thanks for your tips :)
At this moment I'm too use WebView. But this is conceptually wrong, WebView is not for displaying images, it for displaying web content. WebView is bulky and cumbersome. You restricted in customizing it. For example, I need scaling, but I don't want to see zoom controls, or I want to move them in other place then default.
Therefore, I searched about this problem and found some solutions:
PhotoView widget from Chris Banes
Also, you can look at this tutorial from Sony Ericsson (sources available) and can implement your own widget: part1, part2, part3
And another one library ImageViewZoom at github
UPDATE
Chris Banes was created awesome library which supports gestures for zooming pictures, see first point above
The easiest way to handle images is using a WebView, if the image is stored local or somewhere online. WebView supports pinch to zoom and other functions.
Example Java:
String imageUrl = "file:///local/dir/image.jpg"; // http://example.com/image.jpg
WebView wv = (WebView) findViewById(R.id.yourwebview);
wv.getSettings().setBuiltInZoomControls(true);
wv.loadUrl(imageUrl);
XML source:
<WebView android:id="#+id/yourwebview"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Why is WebView unable to open some local URLs (Android)?

I have a WebView that I'm using to open some files stored in the assets/ directory of my project. It works fine for most of the files, but there's one in particular (and I'm sure others I haven't found) that it just will not open.
The file I'm having problems with is named:
"assets/ContentRoot/Photos/XXX Software Logo - jpg - 75%.JPG"
When I pass it to WebView, and it shows the error page, it shows it as:
"file:///android_asset/ContentRoot/Photos/XXX%20Software%20Logo%20-%20jpg%20-%2075%.JPG"
I then tried running URLEncoder.encode() on it and got the error page with the URL presented as:
"file:///android_asset/ContentRoot/Photos/XXX+Software+Logo+-+jpg+-+75%.JPG"
Neither of these URLs were able to open the file (and they both look okay to me). Anyone have any ideas?
UPDATE: If I encode the % by hand (using %25, as commonsware.com suggested) then it loads the image, but it tries to parse it as text, not as an image, so I just get a lot of (basically) garbage.
Also, referring to the image in an HTML document with a relative URL isn't working (probably because it's not being parsed as an image?):
<img src="../Photos/XXX%20Software%20Logo%20-%20jpg%20-%2075%.JPG" />
<img src="../Photos/XXX%20Software%20Logo%20-%20jpg%20-%2075%25.JPG" />
Okay, after spending way too long on this, I've figured out what's going on. Basically, if images stored in the assets/ directory contain a space (e.g., " ") in their file name, they won't render as images.
myWebView.loadUrl("file:///android_asset/testimage.jpg");
works fine. However,
myWebView.loadUrl("file:///android_asset/test+image.jpg");
just throws a not found error and
myWebView.loadUrl("file:///android_asset/test image.jpg");
// and
myWebView.loadUrl("file:///android_asset/test%20image.jpg");
show it improperly displayed (as text... see screenshot in question).
This unexpected behaviour is present on (at least) 1.5, 1.6, and 2.0 and I filed a bug report.
Try getting rid of the % in the filename. Or, escape it as %25.
I would guess that WebView only understands text related content types so it faithfully treating your JPG as base64 encoding, decodes and displays resulted gobble-goop as text. I don't really know if it's possible to set content type for WebView but as workaround you can try to throw img tag inside html tag and load resultet page. Also you probably can only use WebView#loadDataWithBaseUrl

Categories

Resources