Java 为什么 BitmapFactory.decodeByteArray 返回 null?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6520745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does BitmapFactory.decodeByteArray return null?
提问by sandalone
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);
采纳答案by Jon Skeet
From the documentation:
从文档:
ReturnsThe decoded bitmap, or null if the image could not be decode.
返回解码后的位图,如果图像无法解码,则返回null。
The bytes involved in the string "test" aren't a valid bitmap, are they?
字符串“test”中涉及的字节不是有效的位图,是吗?
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'ta valid image in any known format.
如果您将文本“test”保存在名为foo.png
or foo.jpg
etc的文件中并尝试在 Windows 中打开它,您希望结果是什么?这将是一个错误:这些字节根本不是任何已知格式的有效图像。
EDIT: I don't know anything about Android graphics, but your update certainly lookslike a much more reasonable way to draw text onto a bitmap.
编辑:我对 Android 图形一无所知,但您的更新看起来确实是在位图上绘制文本的更合理的方式。
回答by aioobe
Because the bytes in "test".getBytes()
doesn't represent a valid bitmap.
因为中的字节"test".getBytes()
不代表有效的位图。
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.
您需要创建一个字节数组,它实际上包含一个编码位图,而不仅仅是一些与字符串表示相对应的“随机字节”。
回答by inazaruk
You get null
because you supply invalid bitmap data.
你得到null
是因为你提供了无效的位图数据。
See documentation of BitmapFactory.decodeByteArray().
回答by Mark Allison
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.
您正在尝试将 String 解析为位图。BitmapFactory.decodeByteArray()
除非字节数组中存在有效位图,否则将失败。在这种情况下没有,所以它返回 null。
回答by Mustafa Güven
In such case you need to convert the string to Base64 first.
在这种情况下,您需要先将字符串转换为 Base64。
String strImage = geTImageAsHexString();
byte[] x = Base64.decode(strImage, Base64.DEFAULT); //convert from base64 to byte array
Bitmap bmp = BitmapFactory.decodeByteArray(x,0,x.length);
回答by Vlad
byte array of compressed image data - what is this and how it is different from byte[] data = new byte[sz]?
压缩图像数据的字节数组 - 这是什么以及它与 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
至今没有人给出明确的答案!人们一直在谈论的是有一个无效的位图。一个更有用的答案是如何在低级别创建一个有效的字节数组
回答by Jevgenij Kononov
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.
在我的情况下 BitmapFactory.decodeByteArray 返回 null 因为接收到的图像缓冲区不正确。尝试查看发送缓冲区和传入缓冲区,我相信您会看到两个数组的区别。大多数时候这是原因。