15

It's the simple code and instead of getting result to set the Bitmap, I get null. Can anyone tell me where I am making a mistake?

String test = "test";
byte[] byteA = test.getBytes();
Bitmap bmp = BitmapFactory.decodeByteArray(byteA, 0, byteA.length); //<- I get null here
ImageView image = (ImageView) findViewById(R.id.image);
image.setImageBitmap(bmp);

UPDATE

Ok, so I cannot convert text to image like I thought I could. How about this way? Will this create a bitmap?

  Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.RED);
    paint.setTextSize(16);
    paint.setAntiAlias(true);
    paint.setTypeface(Typeface.MONOSPACE);

    Bitmap bm = Bitmap.createBitmap(16, 16, Bitmap.Config.ALPHA_8);
    float x = bm.getWidth();
    float y = bm.getHeight();
    Canvas c = new Canvas(bm);
    c.drawText("Test", x, y, paint);
sandalone
  • 41,141
  • 63
  • 222
  • 338

7 Answers7

23

From the documentation:

Returns The decoded bitmap, or null if the image could not be decode.

The bytes involved in the string "test" aren't a valid bitmap, are they?

If you saved the text "test" in a file called foo.png or foo.jpg etc and tried to open it in Windows, what would you expect the result to be? It would be an error: those bytes simply aren't a valid image in any known format.

EDIT: I don't know anything about Android graphics, but your update certainly looks like a much more reasonable way to draw text onto a bitmap.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I know that. I've read it :). I meant, why it does not create bitmap? Where is the mistake in the code? – sandalone Jun 29 '11 at 12:41
  • The string "test" is `74657374` in hexadecimal. You are trying to render that as a bitmap. It isn't one. – yep Jun 29 '11 at 12:43
  • 3
    @askmoo: The mistake is in trying to read four bytes as if they were an image. What image format do you think those bytes are in? PNG? JPEG? What would you expect the image to be? Note that you *aren't* loading a file called "test" or anything like that... you're just trying to create an image from the bytes representing the string "test" in the platform default encoding. – Jon Skeet Jun 29 '11 at 12:43
  • 1
    @askmoo, you say you know what `decodeByteArray` does, but your question and subsequent comments suggest otherwise. You asked what you are doing wrong, and you currently have 4 (effectively identical) answers telling you exactly what you have done wrong so I have to ask - what, precisely are you expecting to happen? You are apparently using the wrong approach to solving the problem you have. – RivieraKid Jun 29 '11 at 12:48
  • The question appeared while I was writing that question :). Now I know what was the mistake. – sandalone Jun 29 '11 at 12:55
  • @JonSkeet hi sir I am getting the valid byte array and when I save it as png file it shows the image but when I try to create Bitmap using BitmapFactory.decodeByteArray it returns null.....here is the question http://stackoverflow.com/questions/14681643/image-isnt-creating-using-the-bitmapfactory-decodebytearray – Pragnani Feb 04 '13 at 11:44
  • and make sure to remove "data:image/jpg;base64, ...."; using final String pureBase64Encoded = encodedString.substring(encodedString.indexOf(",") + 1); – Zar E Ahmer Nov 15 '18 at 09:26
5

In such case you need to convert the string to Base64 first.

String strImage = geTImageAsHexString();
byte[] x = Base64.decode(strImage, Base64.DEFAULT);  //convert from base64 to byte array
Bitmap bmp = BitmapFactory.decodeByteArray(x,0,x.length);
Adil Malik
  • 6,279
  • 7
  • 48
  • 77
Mustafa Güven
  • 15,526
  • 11
  • 63
  • 83
4

Because the bytes in "test".getBytes() doesn't represent a valid bitmap.

You need to create a byte-array which actually contains an encoded bitmap, not just some "random bytes" corresponding to the representation of a string.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • So how to convert String to byte[] -> byte[] to Image? I thought this is the way to do it. – sandalone Jun 29 '11 at 12:43
  • 2
    @askmoo: What would you expect the image to contain? A picture of the word "test"? It's really not clear what you're trying to do... – Jon Skeet Jun 29 '11 at 12:46
  • @Skeet You are correct. I have edited the code and I will paste the new code soon. Thanks – sandalone Jun 29 '11 at 12:48
4

You get null because you supply invalid bitmap data.

See documentation of BitmapFactory.decodeByteArray().

inazaruk
  • 74,247
  • 24
  • 188
  • 156
3

You're trying to parse a String as a bitmap. BitmapFactory.decodeByteArray() will fail unless there is a valid bitmap in the byte array. In this case there isn't, so it returns null.

Mark Allison
  • 21,839
  • 8
  • 47
  • 46
  • So how to convert String to byte[] -> byte[] to Image? I thought this is the way to do it. – sandalone Jun 29 '11 at 12:43
  • 2
    Create a `Bitmap` using `Bitmap bm = Bitmap.createBitmap();` then create a canvas using `Canvas c = new Canvas( bm );` then create some text in the canvas using `c.drawText()`. The Bitmap will contain your text. You may need to Google for some tutorials on `Canvas` if you're not familiar with it. – Mark Allison Jun 29 '11 at 12:49
  • 1
    You don't actually have a string that will correlate to an image. Do you mean, load a file, based off a name you have in a `String`? – yep Jun 29 '11 at 12:49
  • Your updated code *will* draw the text, but it will be outside the bounds of your bitmap, so will you won't see it. Also, you won't fit very much text in to a 16 x 16 bitmap when you call `setTextSixe( 16 )`. I think that this is now well beyond the bounds of the original question, and if you require any further help, I would suggest starting a new question. – Mark Allison Jun 29 '11 at 13:10
1

byte array of compressed image data - what is this and how it is different from byte[] data = new byte[sz]?

Nobody so far gave clear answer! All that people been talking about is that there is an invalid Bitmap. A more informative answer would be how to create a valid byte array on the low level

Vlad
  • 4,425
  • 1
  • 30
  • 39
0

In my case BitmapFactory.decodeByteArray returned null because received image buffer was incorrect. Try to see sending buffer and incoming buffer, I am sure you will see the difference in two arrays. Most of the time this is the cause.

Jevgenij Kononov
  • 1,210
  • 16
  • 11