Bitmap from String

前端 未结 3 506
情歌与酒
情歌与酒 2021-01-20 21:13

Is there a way or How do I take a String and create a bitmap from it using Java for development for Android?

I had a look at the java api for bitmaps and couldnt fin

3条回答
  •  臣服心动
    2021-01-20 22:02

    Assuming that your image data is in a String called myImageData, the following should do the trick:

    byte[] imageAsBytes = Base64.decode(myImageData.getBytes());
    ImageView image = (ImageView)this.findViewById(R.id.ImageView);
    image.setImageBitmap(
            BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
    );
    

    For Base64 decoding, you can use http://iharder.sourceforge.net/current/java/base64/ as Android doesn't contain Base64-support prior to 2.2.

    Note, I didn't actually run this code, so you'll have to doublecheck for errors.

    more link: using canvas http://developer.android.com/reference/android/graphics/Canvas.html

提交回复
热议问题