sAleksovski / react-native-android-widget

Build Android Widgets with React Native
https://sAleksovski.github.io/react-native-android-widget/
MIT License
578 stars 22 forks source link

ImageWidget support svg network images #73

Closed justinkx closed 6 months ago

justinkx commented 6 months ago

When try to include svg image network image src path, it throws error Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference

Is it possible that modifying getBitmapFromURL alone will support svg network images ?

 private Bitmap getBitmapFromURL(String src, int width, int height) {
        try {
            Bitmap bitmap = null;
            if (src.toLowerCase().endsWith(".svg")) {
                if(ResourceUtils.isResource(src)) {
                    SVG svg = SVG.getFromAsset(appContext.getAssets(), src);
                    bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(bitmap);
                    canvas.drawPicture(svg.renderToPicture(), new Rect(0, 0, width, height));
                } else {
                    SVG svg = SVG.getFromInputStream(new URL(src).openStream());
                    bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(bitmap);
                    svg.renderToCanvas(canvas);
                    bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
                } 
            } else {
                bitmap = ResourceUtils.getBitmap(appContext, src);
                if (bitmap != null) {
                    bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
                }
            }

            return bitmap;
        } catch (IOException e | SVGParseException e) {
            e.printStackTrace();
            return null;
        }
    }
sAleksovski commented 6 months ago

Maybe, but svg images should be used with the SvgWidget.

I just released 0.12.0 which supports network svg files.

You can see the updated docs at https://saleksovski.github.io/react-native-android-widget/docs/primitives/svg-widget