Siju
Siju

Reputation: 2655

Decoding SVG image to bitmap

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); 

Upvotes: 17

Views: 31650

Answers (7)

Caleb Koch
Caleb Koch

Reputation: 874

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)

Upvotes: 0

Sadique Khan
Sadique Khan

Reputation: 320

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)

Upvotes: 1

Benjamin Basmaci
Benjamin Basmaci

Reputation: 2557

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

val yourBitmap = getDrawable(R.drawable.svgimage)!!.toBitmap(width, height)

Upvotes: 13

Style-7
Style-7

Reputation: 1209

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 );

Upvotes: 0

Harsh Mittal
Harsh Mittal

Reputation: 3018

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);

Upvotes: 22

Ganpat Kaliya
Ganpat Kaliya

Reputation: 898

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);

Upvotes: -1

Mariano Zorrilla
Mariano Zorrilla

Reputation: 7686

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);

Upvotes: 3

Related Questions