0

My API is returning an SVG, I want to take this SVG and turn it into a bitmap, so I can use it as a Pin on my google maps fragment.

I found https://github.com/japgolly/svg-android which as soon as I added the Jar to my app I started to get weird runtime errors related to fonts. Clearly it is too out of date to be of any use.

I looked into Glide because some people thought it would work with SVGs.

I don't even have a datatype to read it in, and really no way to convert it to a usable format.

All I want to do is take this responseBody.byteStream() and turn it into a bitmap. That said a Java solution must also exist.

 public Observable<Bitmap> fetchBitmap(String url) {
    Observable<Bitmap> bitmapObservable = mGenericApiService.getBitmap(url)
        .map(responseBody -> {
          Bitmap.Config conf = Bitmap.Config.ARGB_8888; 
          Bitmap bitmap = Bitmap.createBitmap(50, 50, conf);
          //******** CODE HERE?? ********
          return bitmap;
        }).doOnError(getUniversalErrorHandler(mContext, mEventBus));
    return bitmapObservable;
  }
StarWind0
  • 1,554
  • 2
  • 17
  • 46
  • See https://github.com/BigBadaboom/androidsvg also https://github.com/jiahuanyu/SVGMapView?utm_source=android-arsenal.com&utm_medium=referral&utm_campaign=1923 – Manohar Feb 22 '18 at 08:08

1 Answers1

0

What you're missing is something like the below (not tested):

public Observable<Bitmap> fetchBitmap(String url) {
    Observable<Bitmap> bitmapObservable = mGenericApiService.getBitmap(url)
        .map(responseBody -> {
          Bitmap.Config conf = Bitmap.Config.ARGB_8888; 
          Bitmap bitmap = Bitmap.createBitmap(50, 50, conf);

          // Get a Canvas for the Bitmap
          Canvas  canvas = new Canvas(bitmap);

          // Read the SVG file
          SVG svg = SVGParser.getSVGFromInputStream(inputstream);
          // There are other ways to read an SVG file. See the SVGParser class for the others.

          // Get a Picture from the SVG and render it to the Canvas
          canvas.drawPicture(SVG.getPicture());

          return bitmap;
        }).doOnError(getUniversalErrorHandler(mContext, mEventBus));
    return bitmapObservable;
}
Paul LeBeau
  • 97,474
  • 9
  • 154
  • 181