Android: How to load a vector drawable image into a webview? - android

Sorry if this is a repeat but I searched diligently and didn't find this question asked yet...
So back in ye olden days of Android, you could just add something like this to a WebView's source html:
<img src='file:///android_res/drawable/my.png'/>
And it would display my.png correctly in the WebView. But-- Android now supports (and prefers we use) these new-fangled vector drawables, and Android Studio's Vector Asset Studio makes it really easy to import them from SVG files or whatever.
The great thing is that for backwards compatibility, .pngs are automatically generated during the build for various dpi/screen sizes to support older devices. So if you're targeting an older device, there are .png images there.
If only I could reach them. Because, as far as I can tell, using vector graphics does break those file:///android_res/drawable/ img links in WebViews. Which is weird, because as I say there are .png files to see, if it only looked.
I've thought about a few methods to fix this, but none have worked:
Since .pngs are generated on-the-fly and are included in the .apk, I thought maybe I could point directly to one of the generated files with something like file:///android_res/drawable-hdpi-v4/my.png. (I checked the .apk file and that's where one of them was..) But no such luck there. Broken image.
Okay, I thought-- maybe there's a way to get the WebView to display the vector objects directly. That is, maybe the WebView would recognize an <img src="file:///android_res/drawable/my_image.xml"/> or something similar. I mean, Chrome can render svg right? Maybe it supports the .xml vector format too. But no dice here either. (Even if it had worked, it would be unlikely to support older pre-chromium webview versions.)
The third thing I tried is to include just one xxxhdpi-sized .png in res/drawable so that the webview would find it and hopefully display it. Unfortunately, this doesn't work either. It appears that just having an .xml vector in /drawable is enough to have it block the png, no matter if it's in the /drawable or /drawable-xxxhdpi directories.
So the obvious question: Is there a way to get the WebView to either display the vector xml (or the associated support .png that was generated)? How do we access drawables from HTML in a WebView w/Vector drawables? Esp. without having to do anything programmatically?
Anyone know how this is supposed to work? I guess the next thing to do is delve into the chromium/android source to try to find out how file:///android_res/drawable/ works, but that doesn't sound fun, so if anyone has an answer, that would be helpful.
I'm probably missing something really obvious here. Thx in advance!
Update: Other things I tried/ruled out include (1) removing underscores in image names (as I thought they might be replaced with "/"), (2) various permutations of baseurl in loadDataWithBaseUrl()

Well, four years later and the best solution I've come up with is just to have two vector files-- (1) the xml Android vector file for the app, and then (2) the identical image in the .svg format for the webview. This is still a smaller solution compared with providing a seperate .png or .gif for every dpi, etc.
SVG and the vector format are internally very similar, so it's not difficult to convert one to the other. In fact, Android Studio will convert SVG->XML. If you want to go the other way, just compare a few examples.
So just put your svgs in assets/svg/, and then you can access from a webview using <img src='file:///android_asset/svg/mysvg.svg'/> in your HTML.
There's still a storage hit. But svgs are relatively tiny (and you can minify-- ie, scoop out some of the extra fluff in the SVG file that isn't used by the webview) so it's not as bad as the alternative-- providing large binary image files in the apk.

Related

Can I use SVGs instead of PNGs to large scale project

We've already built projects like DocuSign in both iOS and Android, we've quite extensive use of documents which is loaded in form of PNGs, where users can quickly move to their selected page (e.g 1 -> 25), so we worked on closely to make it stable and fast but we've requirement to not gets pixelated while zooming (where PNG lack and gets pixelated), so I done R&D on vector base supported in iOS and found that PDF will not fit for our requirement but SVG may fit. But As SVGs aren't natively supported in iOS so
Is it the right decision to use any third party for SVG support in iOS
like https://github.com/SVGKit/SVGKit in large scale projects?
Will memory management will effect I read many links where it
mentioned it's not recommended like
https://www.quora.com/Should-you-use-SVG-files-for-images-in-an-iOS-app
?
Does anybody go through this kind of problem? looking for suggestions and solutions.
NOTE: These images are coming from cloud(our server, google drive, dropbox and One drive)
You can use Vector xml
Right click on rec>drawable folder
select Vector Asset
then select Local file(SVG)
Pick path of svg file
and import this will convert your SVG to the vector xml file, this vector drawable you can use in place of PNG files

Reading ICO file in android along with all sub-images

I have found Is there a way to decode a .ICO file to a resolution bigger than 16x16? from 2 years ago and the best suggestion was to use image4j. Unfortunately it does not work under android in particular (also), because the classes "IndexColorModel", "BufferedImage" and "WritableRaster" are not available.
While working around "BufferedImage" by replacing it with "Bitmap" may perhaps work and not using "WritableRaster", but instead setting individual (or a group of) pixels using setPixel may work as well, I cannot manage to replace "IndexColorModel", because I cannot wrap my head around it.
I am currently downloading a favicon from a website, which stores usually more than one image inside of it. The images are of different size. I read up on the structure of ICO files and analyzed image4j as much as I could. Yet I have troubles refactoring the various classes to not use AWT.
BitmapFactory is able to load ICO files; unfortunately it only loads the first image (this is my guess at least) and thus does not let me decide which image to load (let alone load them all and let me chose).
Does anyone know if anything changed from 2 years ago and/or would anyone be willing to help me refactor e.g. BMPDecoder from image4j? Or is there perhaps a totally different, easier approach to it?
I have created a library based on image4j that will allow reading ICO files into a List of Bitmap-objects. In contrast to image4j ico4a does not use any AWT-classes, but instead only makes use of Bitmap / Bitmap.createBitmap.
See https://github.com/divStar/ico4a .
While the library's performance might not be the best as it uses a Bitmap-object's setPixel method in a loop, it gets the work done and it's good enough for me.
In comparison to image4j my library (ico4a) only decodes/reads files. While saving ICO files could be done probably relatively easy, I have not done so since I do not need it myself.
If you have further questions or issues with the library, post them on gitHub and I will see if I can help.

Graphic for Android/iPhone

I'm making a illustrated instruction for how to use an app that will be needed
for Android/iPhone
I'm not much into coding for Android and I though the client just needed the
illustration but he asks:
"We will need the illustration saved to a file that we can run on mobile devices (iPhone/Android) as well as the source code."
Isn't jpg enough? is there some additional code that you android programmers are
aware of?
No. In Android you can just use a Drawable. This can be a number of different file formats, including your jpeg. It may be good to have a look at Android Asset Studio. With this tool you can get a nice zip file for all your different screen densities. If you keep the file structure that asset studio outputs then Android will do all the heavy lifting for you.
It might also help you to know something about 9-patches. This is how Android knows how to resize and stretch your image. Asset Studio has an option to set this as well.

Displaying SVG files in Android

I want to create an app that will display position on some floor plan. Navigation is implementing via WiFi in certain way, I've done it and so now I have a problem of displaying floor plan.
It might be in some vector format, after surfing internet for some time I've decided that it must be svg file.
I found some solutions, but it isn't working for me!
Library svg-android
There is opportunity to display .svg files, but only "simple" files. It works fine only for file in tutorial, but not for any other .svg file (for example, some other file, that you'll create with Inkscape).
So, I decided, that I'll parse .svg file, make DOM from it, somehow get objects and attributes and draw it via OpenGL ES.
Apache Batik
At first glance, very good solution, but there is a problem. Android has some "native" apache libraries and when I try to do something with batik, it throws NoClassDefFoundError, because it's searching not in batik libraries, but in "native" libraries.
Of course, we can add source code in our project, take only batik parser for .svg files and edit it in some way, but there is a lot of work, with same success we can write our own parser.
Tiny Line
There is no trial version, but if we'll see description of how it works for svg files and android, we'll see that there is only rasterization of such files and that's all.
Is there any solution better than writing own parser?
Did anyone come across this problem?
I would suggest using #1. Don't write your own parser. It's just going to be a huge headache.
How detailed does your floor plan have to be? android-svg supports SVG fairly well. It just doesn't have great support for filters or light sources. Your SVG isn't going to have those in them (I hope).
If you don't want to do that, look into quad trees. You can render out a huge image and break that down into a quadtree like format then only load the quads you require.
I have authored an SVG libaray for android the website is
http://www.vectoroid.com
the main thing missing is SVG arc support this is currently fixed and will be in a release in the near future.
I am looking for feedback on it, as i have been working on it for about a year. So if you have any please do tell ...
I've using yet another SVG for Android; seems it is relatively new.
GPLv3, CSS2 support, fonts, texts and spans, linear and radial gradients, SVG and SVGZ, initial filtering support, images from assets, from web and inline base64-encoded images. Fastest from all I've tried.
Of course filters support might be better, but except this it works well and even displays 20-megabytes SVG files.
Successfully tested these 2 libraries:
https://github.com/BigBadaboom/androidsvg - Apache License 2.0
https://scand.com/products/svgkit-android/ - needed tweaking to compile with modern SDK (suppress warnings, update gradle file); LGPL / commercial
androidsvg looks better so far.

Android game Image format

I have a problem with an image for an android game. The problem is not a problem with the code because the code that I use I took from a book (Beginning Android 4 Games Developer).
The problem is this: I know the format that I have to use in android: png, but I don't know the settings for this format that I have to use (like RGB565...). Because if I use simply png, when I run the game the images are not good. So I need someone to explain to which settings I need to use for images for android games.
P.S The software that I used is photoshop. If there is better software for this purpose tell me.
I think there is a strong misconception in your understanding of Android and how it implements graphics. You are not constrained to .png for nearly any of your development. The .png and .9.png are only enforced strictly for managing drawable constants.
Android uses Java and has the capability to utilize nearly any graphical format. In particular native support for .bmp, .png, and .jpg are present for every device and Android OS version. You may even create your graphics in realtime byte by byte.
As for a good image editor, there are a number out there. I often use a combination of GIMP and Photoshop, myself.
Hope this helps,
FuzzicalLogic

Categories

Resources