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

3
  • 1
    what do you mean by string? Are you talking about something like a base64 encode string? Commented Nov 8, 2011 at 5:21
  • just Test, a character array. I just need to create a bitmap that is 96 x 96 px big Commented Nov 8, 2011 at 5: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? Commented Nov 8, 2011 at 5:26

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

DrawText on canvas by creating bitmap.

Comments

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

1 Comment

You could have just pointed here instead of copying it verbatim: stackoverflow.com/questions/3801760/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.