How to show SVG string as an Image in Xamarin? - android

So the simple idea is that I have SVG data as a string that I fetch from Internet.
I would like to show that as an icon in my app. Is there any way I can do this?
I have seen countless examples where SVG data is in a file located in the app's directory that is then showed but this is not what I am looking for. I literally have the data in XML format after http request and I only need to transform that to a Image or something else visible on the screen.
I have been trying to find a solution to this for hours now, so I would really appreciate some help :S

Android doesn't support svg with ImageView directly,you could display SVG with some commonly used third-party controls.
Like FFImageLoading.
Add Xamarin.FFImageLoading.Svg nuget package to platform project. Use ImageService with svg data resolver for displaying svg images.
For Android:
<ImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
then use like:
var svgString = #"<svg><rect width=""30"" height=""30"" style=""fill:blue"" /></svg>";
ImageView imageView = FindViewById<ImageView>(Resource.Id.image_view);
ImageService.Instance
.LoadString(svgString)
.LoadingPlaceholder(placeHolderPath)
.WithCustomDataResolver(new SvgDataResolver(64, 0, true))
.WithCustomLoadingPlaceholderDataResolver(new SvgDataResolver(64, 0, true))
.Into(imageView);

Related

Android Glide load the SVG image directly from the string

I want to load svg image in Android using Glide. In fact Glide support such job and I found a sample https://github.com/bumptech/glide/tree/v3.6.0/samples/svg/src/main/java/com/bumptech/svgsample/app. The question is that I want to directly load the image from a string which is in svg format, not from the svg file. The string is received from network or selected from sqlite. Can someone help me? I have searched a long time but it seems no one meet such requirement.
Your question is unclear. You haven't been that clear on what part you are stuck on.
If you just need to get an InputStream from a String, then you can do:
InputStream stream = new ByteArrayInputStream(svgString.getBytes(StandardCharsets.UTF_8));

show gif image in imageview Android

A very strange behaviour, while writing code for showing GIF image in image-view.
what i am doing is
InputStream is = this.getResources().openRawResource(R.drawable.logo);
logo is gif image that is in the drawable folder.
when I write that line of code in Eclipse , image loads successfully but when I import that code on Android Studio What I am seeing is an error on R.drawable.logo
and the error is
Expected Resource of type raw
supplying the wrong type of resource identifier. For Example when calling Resources.getString(int id), You should be passing R.string.something not R.drawable.something
one code runs on Eclipse not on Android Studio
I am sure that no problem with the imports.
After doing some R&D i have found a work-around for that
and that is instead of creating drawable folder and put that image into it
create assets folder in main/src/assests and paste that image and then open Input steam.
InputStream is = context.getAssets().open("logo.gif");
that worked for me but sill dont know the answer why
InputStream is = this.getResources().openRawResource(R.drawable.logo);
was not working.
you can try using ion https://github.com/koush/ion#load-an-image-into-an-imageview is a nice library for load images and support gif from local store , resorces or from Url.
Simple solution is using Glide library
compile 'com.github.bumptech.glide:glide:4.3.1'
And Load your ImageView
Glide.with(YourActivity.this).load(R.drawable.your_gif).into(yourImageView);

how to create gridview by using JSON images in android

I want to create grid view using JSON.JSON contain image urls.I am reading number of tutorials but i not getting what am want.JSON contain many images(more than 100).please help me how can i solve this issu
I can recommend a different way that works like a charm: Android Query.
You can download jar from here
AQuery androidAQuery = new AQuery(this);
As an example:
androidAQuery.id(YOUR IMAGEVIEW).image(YOUR IMAGE TO LOAD, true, true); // true, true means image caching in file and in sdcard, if you want to show images fast then keep this true.
It's very fast and accurate, and using this you can find many more features like animation when loading, getting a bitmap (if needed), etc.

do i need to load jpg file for flex air mobile projects like as3?

In a typical as3 or flex project, after loading xml file, i load jpg files (thumbnails etc) manually, so i can use them in sprites / movie clips etc..
currently i am working on air mobile project. and i am attempting to load some thumnails(jpg) file to list view (spark) and using custom item renderer.
itemrenderer has an spark image component in it. and its data property is set to Image object.
i can check that image files do exists in file application directory.
do i need to load all those thumbnails in memory. then use them?
image object will autoload source file object?? once assigned?
do i have to tell it explicitly to load file object? what events should i use to amke sure , image file object is loaded.?
any ideas?
thanks in advance.
Spark images are really easy to work with. All you need is a url.
<s:Image source="http://someimagesite.com/someimage.png" width="100%" height="100%" />
You can also use bitmap data or even embed directly into the source tag.
[Embed(source="image.png")] private var myImage:Class
mySparkImage.source = new myImage() as BitmapData;
<s:Image source="#Embed('image.png')" />
Take a look:
http://help.adobe.com/en_US/flex/using/WSc5cd04c102ae3e97-33ad5caa12c719dc7c8-8000.html

How to Set Background Image from asset folder in android?

I am facing problem while setting the backGround Image of LinearLayout from asset folder.
String filePath="file:///android_asset/images/test.png";
Drawable d = Drawable.createFromPath(filePath);
frontTextView.setBackgroundDrawable(d);
Can someone help me out.
First you create a Drawable object from the asset file:
Drawable d = Drawable.createFromStream(getAssets().open(path_in_assets), null);
and then set it to some View that only supports Drawables as a background.
As far as I'm aware, you cannot access assets directly like you are trying to. You'll need to use the AssetManager class to get at your data if you want to store it as an asset. Here's a pretty decent blog post explaining a bit about resources and assets, though the official documentation is also a good resource, of course.
I'll also add, though, that things like background images are typically best stored in res/drawable and accessed using the R.drawable.* style (the blog post linked above also discusses this) whenever possible. It's not really clear why you need to do it this way from your provided code sample, though, so I suppose that's ultimately your call.
EDIT: added create image from InputStream...
I had the similar problem using ImageButton. I figured it out by loading bitmap from assets and using it as image for ImageButton. Probably not a good approach, but is working and solved my problem - unability to have subfolders in drawable dir and not allowed characters in file names.
(Yes, I can use prefix instead of subdir, and rename files to match the pattern (lowercase only and numbers) and I probably will do it later.)
InputStream is = null;
try {
is = this.getResources().getAssets().open("Images/Fruits/Apple.png");
} catch (IOException e) {
Log.w("EL", e);
}
Bitmap image = BitmapFactory.decodeStream(is);
ImageButton ib2 = (ImageButton) findViewById( R.id.imageButton2);
ib2.setImageBitmap( image);

Categories

Resources