這是簡單的代碼,而不是獲取結果來設置位圖,我得到空。誰能告訴我我犯了什麼錯誤?爲什麼BitmapFactory.decodeByteArray返回null?
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
好了,所以我不能將文本轉換爲圖像像我以爲我可以。這樣呢?這會創建一個位圖嗎?
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
字符串「test」是十六進制的「74657374」。您正試圖將其呈現爲位圖。它不是一個。 – yep
@askmoo:錯誤在於嘗試讀取四個字節,就好像它們是圖像一樣。你認爲這些字節在什麼圖像格式? PNG? JPEG?你期望的形象是什麼?請注意,您*不會*加載稱爲「測試」的文件或類似的文件......您只是試圖從平臺默認編碼中表示字符串「test」的字節中創建一個圖像。 –