为什么BitmapFactory.de codeByteArray返回null? [英] Why does BitmapFactory.decodeByteArray return null?
问题描述
这是简单的code和,而不是让结果来设置位图,我得到空。谁能告诉我在哪里,我犯了一个错误?
字符串测试=测试;
byte []的BYTEA = test.getBytes();
BMP位= BitmapFactory.de codeByteArray(BYTEA,0,byteA.length); //< - 我得到空在这里
ImageView的形象=(ImageView的)findViewById(R.id.image);
image.setImageBitmap(BMP);
更新
好了,所以我不能将文本转换为图像像我想我可以。这个怎么样的方式?这是否会创建一个位图?
油漆涂料=新的油漆();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
paint.setTextSize(16);
paint.setAntiAlias(真正的);
paint.setTypeface(Typeface.MONOSPACE);
位图BM = Bitmap.createBitmap(16,16,Bitmap.Config.ALPHA_8);
浮X = bm.getWidth();
浮动Y = bm.getHeight();
帆布C =新的Canvas(BM);
c.drawText(测试,X,Y,油漆);
从<一个href="http://developer.android.com/reference/android/graphics/BitmapFactory.html#de$c$cByteArray%28byte%5B%5D,%20int,%20int%29">the文档:
返回的去codeD的位图,或NULL,如果图像不能去code。
涉及的字符串中的字节测试是不是一个有效的位图,是吗?
如果您保存在一个名为文本测试 foo.png
或 foo.jpg
等,并试图在Windows中打开它,你会希望得到的结果是什么?这将是一个错误。这些字节简单的不是的任何已知格式的有效图像
编辑:我不知道关于Android的图形什么,但你肯定更新的看起来的像一个更合理的方式提请文字到一个位图
。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);
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.
这篇关于为什么BitmapFactory.de codeByteArray返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!