3

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 find anything

Robert de Klerk
  • 624
  • 3
  • 12
  • 24
  • 1
    what do you mean by string? Are you talking about something like a base64 encode string? – blessanm86 Nov 08 '11 at 05:21
  • just Test, a character array. I just need to create a bitmap that is 96 x 96 px big – Robert de Klerk Nov 08 '11 at 05:25
  • You can create a bitmap and set pixel values from any source, including a string. How to do that depends entirely on what's in the string and what you want it to mean. Can you be more specific? – Ted Hopp Nov 08 '11 at 05:26

3 Answers3

3

You can use the decodebytearray method of bitmap factory like

byte[] imageAsBytes = Base64.decode(myImageData.getBytes());
Bitmap bp = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);

Where myImageData is a base64 string.

If you have an array just pass that to the decodeByteArray method.

blessanm86
  • 31,439
  • 14
  • 68
  • 79
0

DrawText on canvas by creating bitmap.

Mal
  • 373
  • 4
  • 15
0

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

Hemant Menaria
  • 701
  • 1
  • 7
  • 17
  • You could have just pointed here instead of copying it verbatim: http://stackoverflow.com/questions/3801760/android-code-to-convert-base64-string-to-bitmap – Scott A Nov 08 '11 at 14:47