2016-05-19 4 views
1

私はAndroidで画像に変換する必要があるSVGバイト配列を持っています。バイト配列は、Base64を使用してエンコードされます。私はAndroidがSVGイメージをサポートしていないと考えています。これは変換メソッドがnullを返す原因となります。とにかくこれができますか?ここでSVGデータのURIからビットマップへ

は私が画像を変換するために使用しようとしていた方法です。

AndroidSVGを使用して
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); 
} 
+0

[サードパーティ製を使用してくださいSVGライブラリ](http://android-arsenal.com/tag/96)。 – CommonsWare

+0

あなたがお勧めするものがありますか? – mattfred

+0

私は近年一人も使っていません、ごめんなさい。 – CommonsWare

答えて

1

、次のような何かをする必要があります:

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; 
} 
関連する問題