Android SVG数据URI到位图
我有一个SVG字节数组,需要在Android中转换为图像。字节数组使用Base64编码。我认为Android不支持SVG图像,这导致我的转换方法返回空值。有没有办法做到这一点 以下是我尝试用于转换图像的方法:Android SVG数据URI到位图,android,svg,bitmap,Android,Svg,Bitmap,我有一个SVG字节数组,需要在Android中转换为图像。字节数组使用Base64编码。我认为Android不支持SVG图像,这导致我的转换方法返回空值。有没有办法做到这一点 以下是我尝试用于转换图像的方法: public static Bitmap imageFromString(String imageData) { String data = imageData.substring(imageData.indexOf(",") + 1); byte[] imageAsByt
public static Bitmap imageFromString(String imageData) {
String data = imageData.substring(imageData.indexOf(",") + 1);
byte[] imageAsBytes = Base64.decode(data.getBytes(), Base64.DEFAULT);
return BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
}
使用AndroidSVG,您需要执行以下操作:
public static Bitmap imageFromString(String imageData)
{
String data = imageData.substring(imageData.indexOf(",") + 1);
byte[] imageAsBytes = Base64.decode(data.getBytes(), Base64.DEFAULT);
String svgAsString = new String(imageAsBytes, StandardCharsets.UTF_8);
SVG svg = SVG.getFromString(svgAsString);
// Create a bitmap and canvas to draw onto
float svgWidth = (svg.getDocumentWidth() != -1) ? svg.getDocumentWidth() : 500f;
float svgHeight = (svg.getDocumentHeight() != -1) ? svg.getDocumentHeight() : 500f;
Bitmap newBM = Bitmap.createBitmap(Math.ceil(svgWidth),
Math.ceil(svgHeight),
Bitmap.Config.ARGB_8888);
Canvas bmcanvas = new Canvas(newBM);
// Clear background to white if you want
bmcanvas.drawRGB(255, 255, 255);
// Render our document onto our canvas
svg.renderToCanvas(bmcanvas);
return newBM;
}
使用。你知道你推荐的一个吗?我最近几年没有使用过,对不起。我能找到的唯一有效的解决方案,谢谢!