Decoding SVG image to bitmap
Asked Answered
R

7

17

I am using Android Studio to convert my SVG image to XML file . It works fine when I try to access it using R.drawable.svgimage but now I need to decode that image to bitmap.

I tried the following. It returns null for the bitmap.

mResId = R.drawable.svgimage
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeResource(
            mContext.getResources(), mResId, options); 
Reorganization answered 22/9, 2015 at 12:13 Comment(1)
SVG files are XML files. Android Studio is not converting anything. Android does not support SVG files natively. You will need to use one of the external libraries to do anything with them. However, If you SVG files are simple enough, you might be able to convert them to VectorDrawables.Minetta
A
12

In the package androidx.core.graphics.drawable there is a function Drawable.toBitmap

val yourBitmap = getDrawable(R.drawable.svgimage)!!.toBitmap(width, height)
Austriahungary answered 11/12, 2019 at 0:16 Comment(0)
G
22

The following code will works perfectly I have used it: Here R.drawable.ic_airport is my svg image stored in drawable folder.

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
     private static Bitmap getBitmap(VectorDrawable vectorDrawable) {
        Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
                vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        vectorDrawable.draw(canvas);
        Log.e(TAG, "getBitmap: 1");
        return bitmap;
    }

      private static Bitmap getBitmap(Context context, int drawableId) {
        Log.e(TAG, "getBitmap: 2");
        Drawable drawable = ContextCompat.getDrawable(context, drawableId);
        if (drawable instanceof BitmapDrawable) {
            return BitmapFactory.decodeResource(context.getResources(), drawableId);
        } else if (drawable instanceof VectorDrawable) {
            return getBitmap((VectorDrawable) drawable);
        } else {
            throw new IllegalArgumentException("unsupported drawable type");
        }
    }

       Bitmap bitmap = getBitmap(getContext(), R.drawable.ic_airport);
Griskin answered 4/1, 2017 at 17:46 Comment(3)
Somebody please provide an explanation of setting bounds only for right and bottom. Why not for all? Why set it at all?Lavernelaverock
And why @TargetApi(Build.VERSION_CODES.LOLLIPOP)? Does it mean that it's not going to work on devices with another api level?Lavernelaverock
As you can see there are two methods, the first method @TargetApi(Build.VERSION_CODES.LOLLIPOP) is for Target lollipop onwards and the other is for rest target versions.Griskin
A
12

In the package androidx.core.graphics.drawable there is a function Drawable.toBitmap

val yourBitmap = getDrawable(R.drawable.svgimage)!!.toBitmap(width, height)
Austriahungary answered 11/12, 2019 at 0:16 Comment(0)
F
3

You can use this library SVG Android and use it this way:

SVG svg = new SVGBuilder()
            .readFromResource(getResources(), R.raw.someSvgResource) // if svg in res/raw
            .readFromAsset(getAssets(), "somePicture.svg")           // if svg in assets
            // .setWhiteMode(true) // draw fills in white, doesn't draw strokes
            // .setColorSwap(0xFF008800, 0xFF33AAFF) // swap a single colour
            // .setColorFilter(filter) // run through a colour filter
            // .set[Stroke|Fill]ColorFilter(filter) // apply a colour filter to only the stroke or fill
            .build();

After that, convert the SVG into a Drawable:

// Draw onto a canvas
canvas.drawPicture(svg.getPicture());

// Turn into a drawable
Drawable drawable = svg.createDrawable();

and then, the drawable into a bitmap:

Bitmap bitmapsvg = BitmapFactory.decodeResource(context.getResources(),drawable);
Feathered answered 22/9, 2015 at 12:19 Comment(3)
Is there not a way to do it without using external library?Reorganization
apparently even google build a library time ago: code.google.com/p/svg-android and continues over: github.com/pents90/svg-android but looks like, yes, you need it.Feathered
Now that Android studio supports conversion to SVG in 1.4 build . android-developers.blogspot.in/2015/09/android-studio-14.html . I hope Google tells us how to get a bitmap out of it without an external library.Reorganization
A
1

first create xml file from svg file for that

  1. right click on drawable
  2. new -> vector asset
  3. select svg file and create xml file

after that use that xml file like below

val bitmap = getDrawable(context, R.drawable.ic_black_camel)!!.toBitmap(width, height)
Atronna answered 17/11, 2022 at 4:58 Comment(0)
S
0

1) Create a VectorDrawable:

VectorDrawable vd = (VectorDrawable) context.getDrawable( R.drawable.ic_00 );

2) Calculate bitmap ratio and size by vd.getIntrinsicWidth(); and vd.getIntrinsicHeight();.

3) Create canvas with the bitmap.

4) Use vd.setBounds( left, top, right, bottom ); as destination rectangle

5) Finally draw:

vd.draw( canvas );
Sallust answered 10/4, 2019 at 19:27 Comment(0)
M
0

To avoid the "Should not call Context.getDrawable or Resources.getDrawable directly" warning, use AppCompatResources instead:

val bitmap = AppCompatResources.getDrawable(context, R.drawable.my_drawable)!!.toBitmap(width, height)
Martz answered 11/4, 2024 at 16:46 Comment(0)
R
-1

try this,

SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.android);
PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Ri answered 22/9, 2015 at 12:27 Comment(1)
Where are SVG and SVGParser from?Nattie

© 2022 - 2025 — McMap. All rights reserved.